K
K
Kirill2020-09-27 21:43:27
Unity
Kirill, 2020-09-27 21:43:27

NullReferenceException: Object reference not set to an instance of an object when looking up an object. What to do?

Throws the error "NullReferenceException: Object reference not set to an instance of an object sceneMan.OnTriggerEnter2D (UnityEngine.Collider2D collision) (at Assets/sceneMan.cs:14)" when I try to find an empty object with a tag or title.
Here is the script:

using UnityEngine;
using UnityEngine.SceneManagement;
public class sceneMan : MonoBehaviour
{
    public bool nStory;
    public int id;
    private void OnTriggerEnter2D(Collider2D collision)
    {
        SceneManager.LoadScene(int.Parse(name));
        if (nStory)
        {
            Debug.Log("ppos"+id);
            Debug.Log(GameObject.Find("Player").name);
            Debug.Log(GameObject.FindGameObjectWithTag("ppos").name);
            GameObject.Find("Player").transform.localPosition = GameObject.Find("ppos" + id).transform.localPosition;
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2020-09-27
@kirilla39

Debug. Surely it was not possible to simply find the object. Why is hard to say. Maybe he's not on stage at all. Or maybe the scene does not have time to load normally.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question