Search Unity

Toggling a GUI Window on/off on mouseclick

Discussion in 'Immediate Mode GUI (IMGUI)' started by R.A.E., Jul 12, 2008.

  1. R.A.E.

    R.A.E.

    Joined:
    Apr 24, 2008
    Posts:
    44
    I have a prefab that is supposed to be a trigger for a window. So if I click on it the window opens. Here is my code:


    Code (csharp):
    1.  
    2. // QuizGUIManager : Toggles QuizGUI on  off
    3.  
    4. var quizGUI : GameObject;
    5.  
    6. function Start () {
    7.     quizGUI.active = false;
    8. }
    9.  
    10.  
    11. function OnMouseDown () {
    12.     yield;
    13.     quizGUI.active = true; 
    14. }
    15.  
    16.  

    Now the variable is expoed in the editor but i can not drag the GUI Gameobject in the slot (selecting it from the list is not working either). So I´m getting the error message :

    UnassignedReferenceException: The variable quizGUI of 'QuizGUIManager' has not been assigned.


    What is the problem here??
     
  2. R.A.E.

    R.A.E.

    Joined:
    Apr 24, 2008
    Posts:
    44
    So nobody can help with this (probably simple) question?? I´m still new to Unity so please can somebody give a hint. I changed the code to the following. But always get the NullReference

    Its the QuizGUIManager Script

    Code (csharp):
    1.  
    2. // QuizGUIManager : Toggles QuizGUI on  off
    3.  
    4.  
    5. function OnMouseDown () {
    6.    
    7.     yield;
    8.    
    9.  
    10. // THAT SEEMS NOT TO WORK
    11.     quizGUI = GameObject.Find ("QuizGUI");
    12.     quizGUI.active = true; 
    13. }
    14.  
    15.  

    I always get the NullReference. My GO is named QuizGUI as you can see on the 2nd pic.

    Pic 1 are the components from the prefab (with the attached QuizGUIManager Script
     

    Attached Files:

  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    GameObject.Find won't find inactive objects. Hence the null reference exception. The usual way to do it is the way you posted in your first message...not sure why you can't drag it into the slot? Or, assuming quizGUI is active when you start, you could do

    Code (csharp):
    1. private var quizGUI : GameObject;
    2.  
    3. function Start () {
    4.    quizGUI = GameObject.Find ("QuizGUI");
    5.    quizGUI.active = false;
    6. }
    --Eric
     
  4. R.A.E.

    R.A.E.

    Joined:
    Apr 24, 2008
    Posts:
    44
    Thanks Eric,

    i did not know that GameObject.Find is not finding inactive Objects ( see learned something again :) ).

    Yeah, it is strange that it s not possible to select it from the list or just drag n drop it in the slot. Maybe somebody else has an idea why this is the case.

    So if i do it the way you wrote, the QuizGUI will be inactive after the start () is executed. So i will have to look for an inactive object again . And will be back to the NullReference again, or ?

    Please correct me if I m wrong.



    Thanks for your help.

    Marc
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, because quizGUI is a global variable, so once you find QuizGUI, you don't need to find it again because quizGUI has the reference to it.

    --Eric
     
  6. R.A.E.

    R.A.E.

    Joined:
    Apr 24, 2008
    Posts:
    44
    Hi Eric,

    sorry to bother you again.

    But I´ m still getting the NullReference as soon as the object is disabled. (using the private var …)



    Also tried to drag the Prefab to the scene (since it seemes to be a problem with a prefab this time with var QuizGUI : GO ; to be able to drag the object to the slot -> which is working in the Scene) Apply Changes to Prefab.
    As soon as I do so the object(quizGUI) gets removed from the slot it is not working. Saying there is no reference. (Of course since for some reason it got removed again). So there is some problem with updating the prefab!


    Its drivin me mad.

    The script is definetly working but i don´t get the prefab updated :(


    Thanks again for helping out