NullReferenceException: Object reference not set to an instance of an object

Hi, I’m trying to add a function that is in another script and arrow it on a button, but it is giving this error,
can you help me?

    public Button previous;
    public ScriptName MyScript;
    void Start() {
        previous.GetComponent<ScriptName>().objectss();
    }

It probably means that previous.GetComponent() returned null, or you forgot to connect previous in the Inspector.

the script is added to the inspector, the error persists

Try this:

public Button previous;
public ScriptName MyScript;
void Start() {
        Debug.Log("Previous is " + previous.name);
        ScriptName sn = previous.GetComponent<ScriptName>();
        Debug.Log("scriptname is " + sn.name);
        Debug.Log("Trying access: " + sn.Objectss().name);
}

And see where it breaks. That’s where you have a missing object

1 Like

Is all these debugging necessary?
because when I add in my version some errors
my version is 5.6
The .' operator cannot be applied to operand of type void’

No, you could just write flawless code. Why don’t you?

You should show the offending line, that looks to be a simple typo. Be advised that whatever I write here is off the top of my head, and not tested in any way. That’s where you come in :slight_smile:

I think for this occasion this has no solution, because I’ve tried the maximum I know