- Home /
How would you make something disapear for a second?
I want my code so my object disappears and when it reappears it's in a different position. i tried to make it so the object is off the screen while the the count is under 2 seconds and then it reappears in one of the teleport spots. Is there a better way to do this?
if (count < 2){
transform.position = new Vector3(600000, 1000, transform.position.z);
count += Time.deltaTime;
}
if (count >= 2){
transform.position = new Vector3 (teleportSpot.transform.position);
}
Answer by doctorseus · Aug 24, 2013 at 05:28 PM
You could disable the rendering of the object. Look here for more information. http://answers.unity3d.com/questions/14165/show-and-hide-a-prefab-or-gameobject.html
or use the SetActive method http://docs.unity3d.com/Documentation/ScriptReference/GameObject.SetActive.html
SetActive is better because the object literally disappears from scene. If only its renderer is disabled, weird collateral effects may result: other objects may collide with it, or - even worse - its children may still be visible (if they have their own renderers).
If using SetActive, remember that most functions in the inactive object stop executing: it must be activated by an external object, or with Invoke. The example code below makes the object disappear when clicked, and reappear 5 seconds after in the same position:
function On$$anonymous$$ouseDown(){
Invoke("Reappear", 5); // will reappear in 5 seconds
gameObject.SetActive(false); // disappears
}
function Reappear(){
gameObject.SetActive(true); // I'm back!
}