- Home /
Question by
dendens2 · Apr 12, 2013 at 12:40 AM ·
javascriptinstantiate
Getting a component from an instantiated prefab.
Hi, I have a zombie spawn through an Instantiate();. How do I access the component/script of a instantiated prefab? I am asking because I want to have the health get higher every wave. Thanks! Also, if it helps...
if(!GameObject.FindWithTag("Zombie"))
{
countDownWave -= Time.deltaTime;
if(countDownWave <= 0)
{
wave ++;
zombieAmmount += zombieIncrease;
for(var i = 0; i < zombieAmmount; i++)
{
Instantiate(zombie, (spawnPosition[Random.Range(0, 3)]) + Vector3((Random.Range (0, spawnOne.renderer.bounds.size.x)) / 2, 0, (Random.Range (0, spawnOne.renderer.bounds.size.x)) / 2), Quaternion.Euler(0,0,0));
}
countDownWave = waveDelay;
}
}
Comment
Best Answer
Answer by The-IT664 · Apr 12, 2013 at 12:45 AM
The Instantiate method returns a reference to the instantiated GameObject. From the game object you can use one of the GetComponent methods on the GameObject to get a reference to the script.
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.html