- Home /
Player collision activates GO
I'm currently working on a game that selects premade rooms for each time the level is loaded, so the layout is "random". The only problem is, the game lags up and freezes upon the start. I am trying to make a script that toggles the spawning game object when the player collides with another object, but I don't know the correct way to make the script toggle a gameobject. Does anyone know the answer to this?
Could you please explain a bit what you mean by toggling game object?
Sorry, I never was sure of what it's proper name was. When the GameObject is selected, it's the check box to the left of the name of the GameObject :/
Answer by Subhajit-Nath · Dec 02, 2012 at 05:21 AM
There is much simpler logic than this. Why not make an array of the different spawning objects (prefabs) and spawn them with a random index?
That's very simple actually.
var spawnObject : GameObject[];
private var randomIndex : int;
function OnCollisionEnter (col : Collision)
{
randomIndex = Random.Range (0,9); //assuming you have 10 different spawning objects
Instantiate(spawnObject[randomIndex], transform.position, transform.rotation);
Destroy(col.gameObject); //destroy the collision object
}
Otherwise if you wish to toggle a gameobject (for example a script) you need to use GetComponent() method. Links >>>
http://answers.unity3d.com/questions/9724/easy-question-how-to-disable-scriptobject.html
http://answers.unity3d.com/questions/39748/disable-script-from-code.html
Hope this helps. Btw, I haven't checked the code if it works.