- Home /
How do I draw back an inactive item ?
I'm a newbie at Unity and Javascript. My player has a flashlight and I can holster it (play an animation and then set it inactive), but when I try to get it back active, it won't work.
Here is my code: #pragma strict
var Flashlight : GameObject;
function Update () {
if (animation.isPlaying == false)
{
animation.CrossFade("Idle");
}
if (Input.GetKey (KeyCode.LeftShift) && Input.GetKey (KeyCode.W))
{
animation.CrossFade("Run");
}
if (Input.GetKeyDown(KeyCode.F))
{
if (Flashlight.active == false)
{
Activate ();
}
else
{
Desactivate ();
}
}
}
function Activate ()
{
Flashlight.SetActiveRecursively(true);
}
function Desactivate ()
{
animation.CrossFade("Holster");
yield WaitForSeconds(0.8);
Flashlight.SetActiveRecursively(false);
}
Note that I'm also receiving an error: "The referenced script on this Behaviour is missing!" I tried fixing it but with no luck.
Is the code attached to the flashlight or a child of the flashlight? And which line of code is causing the error "The referenced script on this Behaviour is missing!"?
Scribe
The code is attached to the flashlight. It doesn't tell me what line is causing the error. The error occurs just after I start playing.
If the code is attached to the flashlight, when you deactivate the flashlight, the code is also deactivated. $$anonymous$$ove the code onto another object or possible a parent of the flashlight to get the activating code to work.
Answer by robertbu · Jun 05, 2013 at 06:39 PM
When a game object is deactivated, it no longer receives Update() calls. Since your script is attached to the flashlight, this script gets deactivated when the flashlight is deactivated. You can fix this by one of:
Moving the script to a game object that is not the flashlight.
Changing the code so that you hide rather than deactivate the game objects. You can set the Renderer.enabled flag true/false on all the game object in the flashlight to show/hide the flashlight.
Put this on an empty game object as the parent of the flashlight and change Desactivate() to not deactivate the empty game object (only the children).
You can move your 'if (Input.GetKeyDown(KeyCode.F)) to a separate function and call it using InvokeRepeating(). InvokeRepeating() does not get deactivated when a game object is deactivated.
Your answer
![](https://koobas.hobune.stream/wayback/20220613103600im_/https://answers.unity.com/themes/thub/images/avi.jpg)