Search Unity

Trying to disable a script on an object.

Discussion in 'Scripting' started by cristo, May 30, 2017.

  1. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Hi, I'm just trying to disable and then enable a Java script on a gameobject after a coroutine using C#.

    The Java script is called customcursor.

    In my commented out sections you can see some failed approaches to avoiding the error of a null reference.

    Anyway if someone can please help me I'd be grateful.


    Code (CSharp):
    1. public class ButtonsTrue : MonoBehaviour {
    2.  
    3.     public GameObject ButtonOne;
    4.     public GameObject ButtonTwo;
    5.     public GameObject MainCamera;
    6.     public float Sseconds;
    7.  
    8.     // Use this for initialization
    9.     void Start() {
    10.  
    11.         //MainCamera.gameObject.GetComponent <customcursor>();
    12.         //GameObject MainCamera = (MainCamera)this.GetComponent(customcursor(MainCamera));
    13.         MainCamera.gameObject.GetComponent<customcursor>().enabled = false;
    14.         ButtonOne.SetActive(false);
    15.         ButtonTwo.SetActive(false);
    16.         StartCoroutine("WaitForButton");
    17.        
    18.     }
    19.  
    20.  
    21.  
    22.     IEnumerator WaitForButton()
    23.     {
    24.     yield return new WaitForSeconds(Sseconds);
    25.    
    26.         ButtonOne.SetActive(true);
    27.         ButtonTwo.SetActive(true);
    28.         MainCamera.gameObject.GetComponent<customcursor>().enabled = true;
    29.     }
    30. }
    31.  
     
  2. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    So what is the issue here?
     
  3. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    There's an error:

    NullReferenceException: Object reference not set to an instance of an object
    ButtonsTrue.Start () (at Assets/Cardboard/Scripts/ButtonsTrue.cs:17)

    In relation to this:

    Code (CSharp):
    1. MainCamera.gameObject.GetComponent<customcursor>().enabled = false;
    I'm trying to make it an instance of an object.




     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Is 'MainCamera' assigned in the inspector? Does the MainCamera game object have a 'customcursor' script on it?
     
  5. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Sure is!

     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, so MainCamera is good, you say.
    And... does this print the message in the log when you run it?
    Code (csharp):
    1.  
    2. customcusor cc = MainCamera.GetComponent<customcursor>();
    3. if(cc == null) print("this was null");
     
  7. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    My suggestion is to not write the line in different ways in the hope that you'll not get the error, and instead learn what NullReferenceException means here. This means going forward you'll be able to fix these issues yourself.

    Code (CSharp):
    1. MainCamera.gameObject.GetComponent<customcursor>().enabled = false;
    A NullReferenceException on this line means that at least one of the 3 references is null.

    i.e. when you call object.something, object is null.

    So in this case it's either

    (1) MainCamera is null
    (2) MainCamera.gameObject is null
    (3) MainCamera.gameObject.GetComponent<customcursor>() is null

    You say that MainCamera is setup in the inspector so it's not (1).
    It's not (2) either as that's just the Unity property returning the GameObject, and incidentally you don't need this part anyway as MainCamera is already a GameObject.
    So it looks like it's (3) which means that the component is not attached to the GameObject.

    As pointed out above, simply printing out the objects is a quick way to see what's null.
     
  8. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    I tried that and it didn't print "this is null" in the console. Its a weird thing. It's an old Java code. I think I can live without using even. It might be fates way of telling me to rethink my approach to the game!

     
  9. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    It's a good point, see I can see the custom cursor when I run the game. The customcursor code is attached and working. I just want to be able to switch it off and have it active or false for various sections of the game. I'm starting to think I mightn't need it. But i might. Anyway, it's a weird one. It's an old Java code, maybe if it's rewritten in C# it might be able to switched off easier. I don't know if that should even make a difference. If it's just a component. Anyway peace, thankyou for your thoughts.