Object Reference not set to an instance of Object

Asked

Viewed 58 times

-1

Good evening, I’m trying to take a function that be in another script and pass it to a button, only to be giving this error that be in the title.

int indice;
public Button Ant;
public ScriptName Script;

void Start() {
    call();
}

void call() {
    if (indice <= 0) { indice = 0; }

    if (indice >= 1) {
        indice = 1;
    }

    if (indice == 0)
    {
        Ant.GetComponent<ScriptName>().anteriorobj();//assim não funciona
        Ant.GetComponent<Script>().anteriorobj();//assim tambem não funcioa
    }
    else if (indice == 1)
    {
    //blablabla
    }
}

public class ScriptName : MonoBehaviour {
    public void anteriorobj()
    {
        Debug.Log("Active");
    }

2 answers

0

You can simply create a property in the class you want to use the class ScriptName

example:

public ScriptName script;

and access like this:

script.anteriorobj();

remembering that you have to drag the ScriptName for the property at the inspector

  • So as being in the description, I wanted to take this function script.anteriorobj and set it on a button variable

-1

  • The variable Ant must be assigned in the Inspector.
  • The Gameobject referenced in the previous variable (Ant) must have the ScriptName.

If any reference is missing it will give the error mentioned.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.