Problem with a simple Script

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,

function Update () {

if (!control.selectMouse){

gameObject.SetActive (true);
}


if (control.selectMouse){

gameObject.SetActive (false);
}

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

Once you turn off the gameObject, the Update functions will stop running. What you can do is this:

var target:GameObject;

function Update () {

 

if (!control.selectMouse){

 

target.SetActive (true);

}

 

 

if (control.selectMouse){

 

target.SetActive (false);

}

Attach this it another gameobject and assign the target. That way you are not enabling/disabling the object with the script.

Hi MagicFrame,

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

Also, do you know about else? :

    if (control.selectMouse)
    {
          gameObject.SetActive (false);
    }
    else
    {
          gameObject.SetActive (true);
    }

Or in your case, you want SetActive to be the opposite of selectMouse, so this would be even cleaner :

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

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.

And I suggest you read the scripting forum FAQ:

There is nothing wrong with asking scripting questions in the scripting forum

thanks tonyd, I will now read the faq scripting. Thanks

That actually wasn’t aimed at you ;), but I do suggest reading the FAQ.

If you are new to scripting, you might want to visit this link:

http://forum.unity3d.com/threads/34015-Newbie-guide-to-Unity-Javascript-(long)

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.

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.