- Home /
on collision, show/hide other model scripting ?
I have a feeling this should be relatively easy, but im new to scripting so please forgive my ignorance.
I'm trying to get a model ive placed in the scene to be hidden, but show itself when a collision with another object is detected.
This is my script, its attached to the collision object :
// initially hides the model var trigVis : boolean = false; //on collision with object. doesnt need to disappear on trigger exit function OnTriggerEnter(other : Collider) //attached prefab { trigVis = true; } //call every frame function Update() { if (trigVis == true) { // im not sure this part is right? transform.Find("gameModel").renderer.enabled = true; } if (trigVis == false) { transform.Find("charDoneComplt").renderer.enabled = false; } }
Answer by Yoerick · Nov 03, 2011 at 08:24 PM
Hi,
First of all, you want to avoid using the Find function inside the Update function. The reason behind this is that it's quite expensive to search your entire scene for a specific object. If you place this inside the Update function, Unity will scan your entire scene for the object, every single frame, making your game way slower. To solve this, create two (gameobject) properties at the beginning of your script (like your trigVis boolean) and simply initiate the two objects in a Start function like so:
var gameModel:GameObject; var charDoneComplt:GameObject;
function Start() { gameModel = transform.Find("gameModel"); transform.Find("charDoneComplt"); }
Now you can use these two objects in the OnTriggerEnter function like so:
function OnTriggerEnter()
{
if(trigVis)
{
gameModel.renderer.enabled = true;
charDoneComplt.renderer.enabled = false;
}
}
Btw, what do these two object actually represent? because you say you want to make an object visible when it collides but in your bit of code you turn off another object? (with renderer.enabled = false)
If you only want to make the colliding object visible you should try attaching this to it:
function Start() { gameobject.renderer.enabled = false; }
function OnTriggerEnter(col:Collider) { gameobject.renderer.enabled = true; }
simple as that :) if you want to do something with the other object (which it's colliding with) you can use col.gameobject.renderer.enabled inside the OnTriggerEnter function.
If you want to, it can be even easier. If you want the object to be invisible by default, you don't actually have to disable the object's renderer in code. If you select the object in Unity there should be a "Renderer" component attached to it, which you can simply turn off by unchecking the checkbox next to it. Then these 4 lines of code will make it visible when it collides:
function OnTriggerEnter(col:Collider)
{
gameobject.renderer.enabled = true;
}
I hope this long answer doesn't confuse you too much, just wanted to give you the reasoning behind the code ;) The only thing you need about this answer is the 4 lines of code at the end ;)
Hope it helps,
Yoerick
apparently I read over it a little too quickly. transform.Find indeed only looks for the object inside the specified transform. It's GameObject.Find that searches everything, my bad ;)
Your answer
Follow this Question
Related Questions
Disable and hide object after collision between player and cube 1 Answer
Object Visibility 2 Answers
Objects Appearing 1 Answer
3D model not showing properly at runtime, 0 Answers
Re-hiding a guiTexture 1 Answer