Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by radler470, Jan 9, 2015.

  1. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    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:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class B1 : MonoBehaviour {
    5.  
    6.     Doer Dd;
    7.    
    8.     void OnMouseDown () {
    9.         Dd.Switch(true);
    10.     }
    11.    
    12. }
    13.  
    B2:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class B2 : MonoBehaviour {
    5.  
    6.     Doer Dd;
    7.    
    8.     void OnMouseDown () {
    9.         Dd.Switch(false);
    10.     }
    11.    
    12. }
    13.  
    Doer:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Doer : MonoBehaviour {
    5.    
    6.     public GameObject Thing;
    7.    
    8.     public void Switch (bool on) {
    9.         if (on)
    10.             Thing.SetActive(true);
    11.         else
    12.             Thing.SetActive(false);
    13.     }
    14.    
    15. }
    16.  
    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?
     
  2. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    "Thing" gameobject is most probably not set in the inspector
     
    radler470 likes this.
  3. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    It is, and I have geometry plugged in, which is why I am confused as to it not working. :(
     
  4. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Post the error from the console please, it will give you a line number which it occurs aswell. Post all.
     
    radler470 likes this.
  5. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    NullReferenceException: Object reference not set to an instance of an object
    B1.OnMouseDown () (at Assets/B1.cs:9)
    UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32)
     
  6. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    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.
     
  7. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    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<Doer>() 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.
     
    radler470 likes this.
  8. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    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).
     
  9. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class B1 : MonoBehaviour {
    4.     Doer Dd;
    5.  
    6.     void Start()
    7.     {
    8.        Dd = GetComponent<Doer>(); //Use this if Doer is on the  
    9.                                                            //same gameobject as B1
    10.          //OR
    11.         Dd = GameObject.FindObjectOfType<Doer>(); //Use this
    12.          //if Doer is somewhere in the scene nut not on the
    13.         //same gameobject
    14.  
    15.  
    16.      
    17. }
    18.  
    19.     void OnMouseDown () {
    20.         Dd.Switch(true);
    21.     }
    22.  
    23. }
    24.  

    OR you could also do this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class B1 : MonoBehaviour {
    4. [SerializeField]
    5.    private Doer Dd;
    6.  
    7.     void OnMouseDown () {
    8.         Dd.Switch(true);
    9.     }
    10.  
    11. }
    12.  
    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.
     
    radler470 likes this.
  10. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86

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