Home > other >  I'm getting an UnassignedReferenceException when the reference is set. I've seen other que
I'm getting an UnassignedReferenceException when the reference is set. I've seen other que

Time:01-28

As the title says i'm getting the UnassignedReferenceException error for a variable already set. Using ScriptableObjects im working on an inventory system(partially from scratch) and im trying to access the EquipmentUi in another class using a GameObject to hold the prefab containing the Character script. There is no issue with this working to access the Character script as shown in the picture because i can access the name. However, when i try and access the EquipmentUI of that character it gives the error. This isnt the last part i need access to but i have figured out that the UI is the part i cant access, i need the script held in it(which has worked before in another class).

The variable is already assigned, there is no other object in my scene with the same script attached, and the code can access other parts of the script i want access to which is why i made a new post after seeing the other posts and not seeing one that had quite the same issue.

enter image description here enter image description here enter image description here

using System.Collections;
using System.Collections.Generic;
using UnitEngine;

[CreateAssetMenu(fileName = "New Character Equipment", menuName = "Inventory/Character/CharacterEquipment)]
public class CharacterEquipmentObject : ScriptableObject
{
[SerializeField]
protected Player user;
[SerializeField]
private GameObject equipmentUser; // Only used to get the user because SOs cant getObject<>
[SerializeField]
public EquipmentObject[] equipment = new EquipmentObject[8];
[SerializeField]
EquipmentDisplay equipmentUI;
//private string[] SlotList = new string[8]{"Helmet", "Shoulders", "Chest", "MainHand", "OffHand", "Ring", "Legs", "Feet"}; may need later
public void start() // must be called in display
{
    user = equipmentUser.GetComponent<Player>(); // set the user to be used in other classes\
    Debug.Log("user in CEO: "   user.characterName); // this displays fine
    Debug.Log("user "   user.characterName   "'s equipment: "   user.EquipmentUI.name); // This is what the editor says is empty when the slot has it assigned
    equipmentUI = user.EquipmentUI.GetComponentInChildren<EquipmentDisplay>();
    Debug.Log("equipmentUI: "   equipmentUI.name);
}

Thanks for any help in advance.

Player class:

public class Player : Character
{
    [SerializeField]
    protected Character[] companions = new Character[3];
    [SerializeField]
    GameObject InventoryUI;
    public GameObject EquipmentUI;
    bool isSelling = false;
    public string characterName;

    protected override void Start() 
    {
        InventoryUI.SetActive(false);
        EquipmentUI.SetActive(false);
        Debug.Log("Player Equipment UI: "   EquipmentUI.GetComponentInChildren<EquipmentDisplay>().name);
        base.Start();
    }
    private void OnTriggerEnter(Collider other) 
    {
        var item = other.GetComponent<InteractItem>();
        if(item)
        {
            characterInventory.addItem(item._item, 1);
            useItem(item._item);
        }    
        Destroy(other.gameObject);
    }


 private void Update()
    {
        if(Input.GetKeyDown(KeyCode.I))
        {
            InventoryUI.SetActive(!InventoryUI.activeSelf);
        }
        if(Input.GetKeyDown(KeyCode.C))
        {
            EquipmentUI.SetActive(!EquipmentUI.activeSelf);
            if(EquipmentUI.activeSelf == true)
            {
                EquipmentUI.GetComponentInChildren<EquipmentDisplay>().updateEquipmentSlots();
            }
        }
    }

CodePudding user response:

Thanks for all the help on here but i figured out it is a ScriptableObjects issue as i went in and changed the way it is run, even putting the function to edit the users InventoryObject into the player itself it still gave the same issue.

In the player function Update() that does not have the InventoryObject(ScriptableObject) the update runs fine but as soon as the player function EmptySlot() is called from the InventoryObject it sends the same error.

Not entirely sure why this is but SOs are unable to access the GameObject that holds the script for the inventory display in them whatsoever. If anyone has a reason as to why this is I would love to know, but i am going to move on from this particular part and change how it works.

CodePudding user response:

You might be experiencing a bug I have been facing where the element variable info does not update to show variable changes.
To force it to update, cause an error in your code. Switch to unity, when it shows you the error go back and fix it, and see if the variable value changes then.

  •  Tags:  
  • Related