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;
		}
	}

1 Answer

1

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.

Thank you!