An object reference is not set to an instance of an object

Hello!
I am running into a weird issue. I know that when that error is thrown 9/10 times it is because something isn’t assigned in the inspector. However, everything is.

Basically, I am instantiating the class that holds that write method as M.
When I call M.Write(gameobject1, “test”, true) it throws an error when the code reaches gameobject1.
I don’t understand why? gameobject1 is assigned in the inspector.

public GameObject Object { get; set; }
    public string ObjectString { get; set; }
    public Text ObjectText { get; set; }
    public void Write(GameObject g, string text, bool TypewriterEffect)
    {
        ObjectString=text;
        Object=g;
        if(TypewriterEffect)
        {
            Debug.Log("Typing!");
            Effect_TypeWrite(text);
        }
        else
        {
            var text_string = Object.GetComponent<Text>();
            text_string.text=text;
        }
    }
    public  IEnumerator Effect_TypeWrite(string text)
    {
        var text_string = Object.GetComponent<Text>();
        foreach(char letter in text.ToCharArray())
        {
            text_string.text+=letter;
            yield return null;
        }
    }
    public void Transition(GameObject former, GameObject wish)
    {
            if(Input.GetKeyDown(KeyCode.Space))
            {
                former.SetActive((false));
                wish.SetActive((true));
            }
    }

To get more information, right before the line you’re getting the error on, print the name of the gameObject this script is on (should just be Debug.Log(name)) and see if maybe you have another copy of this script on another object than the one you expect it to be on.

Also, I would NOT recommend using the identifier ‘Object.’ When you have UnityEngine included, then Object is actually a Unity object (a UnityEngine.Object). While the compiler will keep it straight, you are making a great source of future confusion.

I don’t see any gameobject1 variable in what you posted, so you may need to post that part of your code and as suggested, make sure you didn’t get a second copy of the script on accident.

Also, to add onto what @Kurt-Dekker mentioned about naming a property Object, you might also want to consider better variable names in the future. It might not seem like a big deal now to use simple name like g and m, but trust me when I say that’s not a habit you want to get into. Just a friendly suggestion :slight_smile: