Search Unity

Problem with a simple Script

Discussion in 'Scripting' started by MagicFrame, Jul 19, 2013.

  1. MagicFrame

    MagicFrame

    Joined:
    Jul 18, 2013
    Posts:
    42
    Hi All, I am a new user of Unity, I have one problem, I make a simple code for my project, but the script not found well,



    Code (csharp):
    1.  
    2.  
    3. function Update () {
    4.  
    5. if (!control.selectMouse){
    6.  
    7. gameObject.SetActive (true);
    8. }
    9.  
    10.  
    11. if (control.selectMouse){
    12.  
    13. gameObject.SetActive (false);
    14. }
    15.  
    16.  
    17.  
    18.  
    Ok, my problem is with SetActive, when I have Click on the boolean "selectMouse" the SetActive change of false correct, BUT when I have click again the SetActive not return to true! thanks
     
  2. PAEvenson

    PAEvenson

    Joined:
    Nov 12, 2009
    Posts:
    47
    Once you turn off the gameObject, the Update functions will stop running. What you can do is this:

    Code (csharp):
    1. var target:GameObject;
    2.  
    3. function Update () {
    4.  
    5.  
    6.  
    7. if (!control.selectMouse){
    8.  
    9.  
    10.  
    11. target.SetActive (true);
    12.  
    13. }
    14.  
    15.  
    16.  
    17.  
    18.  
    19. if (control.selectMouse){
    20.  
    21.  
    22.  
    23. target.SetActive (false);
    24.  
    25. }
    Attach this it another gameobject and assign the target. That way you are not enabling/disabling the object with the script.
     
  3. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    Hi MagicFrame,

    When you deactivate an object, it will stop being updated ( function Update will NOT be executed ).

    Also, do you know about else? :

    Code (csharp):
    1.  
    2.     if (control.selectMouse)
    3.     {
    4.           gameObject.SetActive (false);
    5.     }
    6.     else
    7.     {
    8.           gameObject.SetActive (true);
    9.     }
    Or in your case, you want SetActive to be the opposite of selectMouse, so this would be even cleaner :
    Code (csharp):
    1. gameObject.SetActive (!control.selectMouse);
    In any case, this has no place in the Update function. I suggest you do some simple scripting tutorials, and post questions on Unity Answers as the forum is for discussions.

    All the best,

    Gregzo
     
  4. MagicFrame

    MagicFrame

    Joined:
    Jul 18, 2013
    Posts:
    42
    thank you both, I'll try your advice, and I've really seen many hours of tutorials, but none were saying that the functions cease to happen once the gameobject has been disabled, today I learned something new, thanks to the two.

    PS: I try but not found.
     
  5. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
    And I suggest you read the scripting forum FAQ:

    There is nothing wrong with asking scripting questions in the scripting forum
     
  6. MagicFrame

    MagicFrame

    Joined:
    Jul 18, 2013
    Posts:
    42
    thanks tonyd, I will now read the faq scripting. Thanks
     
  7. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
  8. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    Apoligies tonyd, I was under the (mistaken) impression that Unity Answers would be better suited for that type of question.

    If not, what is the difference between answers and forum?

    Anyway, didn't want to seem blunt.
     
  9. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
    In a perfect world, everyone would search Unity Answers (as well as the Unity Script Reference) before posting, but this forum is still the quickest way to get answers to scripting questions.