Having some issues switching between two canvases, variables unassigned.

I’m trying to switch between two canvases, I used the thread, How do I reference a canvas? , to come up with this code:

    public Canvas p1;
    public Canvas p2;
    void start(){
        GameObject tempObject1  = GameObject.Find("Programs1");
        GameObject tempObject2  = GameObject.Find("Programs2");
        p1 = tempObject1.GetComponent<Canvas> ();
        p2 = tempObject2.GetComponent<Canvas> ();
    }
    public void forward1(){
        p1.enabled = false;
        p2.enabled = true;
    }

There are no errors directly in the code, but when I press the button assigned with this script I get this error:
UnassignedReferenceException: The variable p1 of programCanvases has not been assigned.
You probably need to assign the p1 variable of the programCanvases script in the inspector.
This same error also applies to the variable p2.

start should be Start. Watch for caps

Also note that GameObject.Find is slow and should be replaced. Really, since your variables are public, you can just drag and drop the Canvas objects into the variables in the inspector instead of doing this through code.

1 Like