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.

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.