1

I am making a multiplayer game in which the characters can change scenes. I have items scattered in different scenes which the player can pick up and add to his inventory. The items have an 'AddItem' script attached to them. If the player is in the first scene and picks up the item, it is added to the inventory, however when the player changes the scene and picks up an item in that scene (which is using same script), it throws null reference error. Here is the script:

public class AddItem : MonoBehaviour
{
    public Item item;
    DialogueConditions dc;
    private GameObject player;

    private void Start()
    {
        player = GameObject.Find("Engineer");
        dc = player.GetComponent<DialogueConditions>();
    }
    void pickUp()
    {
        Debug.Log("Picking up : " + item.name);
        HasBriefDoc();
        bool itemPickedUp = Inventory.instance.Add(item);
        if(itemPickedUp == true)
        {
            Destroy(gameObject);
        }
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            pickUp();
        }
    }
    public void HasBriefDoc()
    {
        if (item == null)
        {
            dc.hasBrief = false;
        }
        else if (item.name == "Brief Doc")
        {
            dc.hasBrief = true;
        }
        else
        {
            dc.hasBrief = false; //throws null reference
        }
    }
}

After debugging, I found out that after scene change, the object 'dc' is assigned with null reference in the Start() method. I also tried initializing it with FindObjectOfType<DialogueConditions>(); and adding the start code to the Awake() method, but the problem still persists. The only way it is working is when I drop the 'dc' object and directly use static variables, which is something I do not prefer.

OddCommand24
  • 371
  • 2
  • 12
  • https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html – aybe Mar 27 '22 at 12:38
  • @aybe still not working – OddCommand24 Mar 27 '22 at 12:54
  • Then as it's private, add `[SerializeField]` to it. – aybe Mar 27 '22 at 13:26
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – BugFinder Mar 27 '22 at 14:42
  • you can put the code of the Start method in the OnSceneLoad method of this example: https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-sceneLoaded.html However, if you have a menu scece, for instance, it would throw an exception because it is also called there. – Soroush Hosseinpour Mar 27 '22 at 18:09

1 Answers1

1

To anyone who is facing this issue, my problem was occurring due to wrong execution order of functions. The 'AddItem' script was running before my player had even instantiated, on which this DialogueConditions script was attached. That is why it was returning null reference. I changed the order of execution by instantiating my player in the Awake() method, as it will then run before the Start() method of AddItem, hence the null reference exception was removed.

OddCommand24
  • 371
  • 2
  • 12