[SOLVED] Issue with Switch statement? Maybe? Please help!

Thanks to user @Karwler I have made a lot of headway on this issue, but I am getting an error that is keeping my script(s) from working properly.

In short, I want to make an object appear by clicking on an object, and disappear by clicking on another object.

I have three scripts:

B1:

using UnityEngine;
using System.Collections;

public class B1 : MonoBehaviour {

    Doer Dd;
   
    void OnMouseDown () {
        Dd.Switch(true);
    }
   
}

B2:

using UnityEngine;
using System.Collections;

public class B2 : MonoBehaviour {

    Doer Dd;
   
    void OnMouseDown () {
        Dd.Switch(false);
    }
   
}

Doer:

using UnityEngine;
using System.Collections;

public class Doer : MonoBehaviour {
   
    public GameObject Thing;
   
    public void Switch (bool on) {
        if (on)
            Thing.SetActive(true);
        else
            Thing.SetActive(false);
    }
   
}

And I am getting this error, despite the fact that I do have an object referenced. The error seems to be landing on the Dd.switch statement. Any ideas why this might be happening? Or have a simpler solution to this task?

“Thing” gameobject is most probably not set in the inspector

1 Like

It is, and I have geometry plugged in, which is why I am confused as to it not working. :frowning:

Post the error from the console please, it will give you a line number which it occurs aswell. Post all.

1 Like

NullReferenceException: Object reference not set to an instance of an object
B1.OnMouseDown () (at Assets/B1.cs:9)
UnityEngine.SendMouseEvents:smile:oSendMouseEvents(Int32, Int32)

I’m getting the exact same error on a fresh scene with primitive objects as well. Can’t seem to figure out why it is not recognizing my GameObject.

Oh god of course, I don’t know how I missed that.

Doer Dd; is never initialised, it will be null in both B1 and B2 scripts. You’ll need to either GetComponent() or pass in a reference to the Doer script via the inspector.

GetComponent will requite Doer to be on the same Gameobject as the B1 and B2 scripts.

1 Like

Thank you so much! Can you show me how that would look in one of the scripts? (So sorry, I am an idiot when it comes to scripting).

using UnityEngine;
using System.Collections;
public class B1 : MonoBehaviour {
    Doer Dd;

    void Start()
    {
       Dd = GetComponent<Doer>(); //Use this if Doer is on the   
                                                           //same gameobject as B1
         //OR
        Dd = GameObject.FindObjectOfType<Doer>(); //Use this
         //if Doer is somewhere in the scene nut not on the
        //same gameobject


      
}
  
    void OnMouseDown () {
        Dd.Switch(true);
    }
  
}

OR you could also do this:

using UnityEngine;
using System.Collections;
public class B1 : MonoBehaviour {
[SerializeField]
   private Doer Dd;
  
    void OnMouseDown () {
        Dd.Switch(true);
    }
  
}

Then in the inspector when you select the GameObject containing script B1, there will be a space requesting a Doer Component. Drag the gameobject that has your Doer component into this slot to set that variable.

1 Like

YES!!! By George you’ve done it!!! THANK YOU THANK YOU!!!