Inspector clears every time I re-open Unity

jstoffer

New member
I wrote a custom action for my hot bar. But now I am having a weird and annoying issue where every time I re-open my unity. The inspector for my script clears out so I have to constantly go back to it and re-populate all the links. How do I fix this so they stay populated?
Untitled.png
Here is my code for the custom action:

Code:
using Opsive.Shared.Game;
using Opsive.UltimateInventorySystem.Core.AttributeSystem;
using Opsive.UltimateInventorySystem.Core.DataStructures;
using Opsive.UltimateInventorySystem.ItemActions;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class UseTool : ItemAction
{
    [SerializeField] protected string m_AttributeName = "ThrowablePrefab";
    public Transform FirePoint;
    public GameObject Body;
    PlayerAnim PA;

    protected override bool CanInvokeInternal(ItemInfo itemInfo, ItemUser itemUser)
    {
        if (itemInfo.Item.GetAttribute<Attribute<GameObject>>(m_AttributeName) == null)
        {
            return false;
        }
        return true;
    }

    protected override void InvokeActionInternal(ItemInfo itemInfo, ItemUser itemUser)
    {

        PA = Body.GetComponent<PlayerAnim>();
        if (PA.Anim.GetInteger("State") == 0 || PA.Anim.GetInteger("State") == 2 || PA.Anim.GetInteger("State") == 5 || PA.Anim.GetInteger("State") == 6) {

            var item = itemInfo.Item;
            var MyPrefab = item.GetAttribute<Attribute<GameObject>>("ThrowablePrefab").GetValue();
            var Value = item.GetAttribute<Attribute<int>>("Type").GetValue();
            var clone = ObjectPool.Instantiate(MyPrefab, FirePoint.transform.position, FirePoint.transform.rotation);
            //    Debug.Log(Value);
            PA.mode = Value;
        }
    }
}
 
Last edited by a moderator:
(When sharing code please use code quotations to make it easier to read, I did it for you this time)

ItemActionSets are scriptable objects. In Unity Prefabs, scriptable objects and all other asset types cannot serialize references to objects that are inside a scene.
So your issue isn't a bug, that's just how Unity works.

So the work around instead of using Transform and GameObject types, is to get those references through code. There are many ways to do so. The simplest one is adding a custom component with all the references you need next to your Inventory and then do a itemUser.GetComponent<MyObject>() within your item action.
The other solution is to use a Identification system, adding IDs to the objects you need and then have a singleton that manages those.

I hope that helps
 
Top