- Home /
Materialize an object, then destroy it.
I need an object to appear as player walks into a trigger. So basicly I am asking for just one line of a script to make an object materialize in front of a player and then to be destroyed. Here is a reference to what I want to achieve.
public var AnObject : GameObject;
function OnTriggerEnter(other : Collider){
if(other.gameObject.name == "Player"){
------------------------????
yield WaitForSeconds(3);
GameObject.Destroy(AnObject);
}
}
Here is the script. Shows no errors. However doesn't work.
#pragma strict
public var AmbienceCrate : GameObject;
function Awake (AmbienceCrate)
{
renderer.enabled = false;
}
function OnTriggerEnter(other : Collider){
if(other.gameObject.name == "Player"){
renderer.enabled = true;
yield WaitForSeconds(3);
GameObject.Destroy(AmbienceCrate);
}
}
Answer by VesuvianPrime · Sep 15, 2014 at 08:54 PM
The following should be an excellent read for you: http://docs.unity3d.com/Manual/InstantiatingPrefabs.html
I sure will but it would be nice to get an answer as well as a direction in which to move. Thanks thou.
Hey osmile
I didn't mean for the link to be dismissive. The documentation page gives some great tips and tricks that explain how to instantiate different types of objects in the scene.
Give a man a fish vs $$anonymous$$ch a man to fish, etc
The other guys are taking a different angle on the problem:
If we assume that your "materialize" object is already in the scene, you can hide it by simply disabling the renderer
You can also enable/disable components via code:
public GameObject materializeObject;
...
materializeObject.renderer.enabled = true;
I got it! Here is a working script! Thank you so much.
public var AmbienceCrate : GameObject;
function Start ()
{
AmbienceCrate.SetActive(false);
}
function OnTriggerEnter (Col : Collider)
{
if(Col.tag == "Player")
{
AmbienceCrate.SetActive(true);
yield WaitForSeconds(3);
GameObject.Destroy(AmbienceCrate);
}
}
I'm glad you got it working. Just keep in $$anonymous$$d that you are disabling the entire GameObject, which may or may not be exactly what you want.
I'm pretty sure it's fine for me. I only need it to appear once and won't need it anymore.
Your answer
![](https://koobas.hobune.stream/wayback/20220613163056im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to make an enemy appear then disappear. (if possible with audio) 1 Answer
GameObject can disappear 1 second after play, but still appear in scene window 1 Answer
Move another object 1 Answer
cannot see game objects on maximise on play 1 Answer
How to make an gameobject dissapear when player gets close to it 0 Answers