Instantiating a non-Transform

Hello, total noob here - I suspect the following problem is me failing to understand how Unity “does things”.

So, I’m building a Tower Defence game and I want to spawn a wave of creeps. In my object hierarchy I’ve built an “Ant” with a mesh underneath it. Attached to Ant is the CharacterMotor and AntThink, its simple AI.

I have a separate item in the hierarchy called “AntSpawner”. Its job is just to spawn Ants at its current location.

I can instantiate the Ants no trouble like so:

void Start () {
	ant = GameObject.FindGameObjectWithTag("ant");
	antt = ant.transform;
}

void Update () {	

	if (Time.fixedTime < nextspawn)
		return;
	
	nextspawn = Time.fixedTime + 2;
	
	Transform clone = Instantiate(antt, this.transform.position, this.transform.rotation) as Transform;

However, this only gives me the ant as a Transform – what I actually want to do is now tweak the properties of the ant as specified in the AntThink class. Unity won’t allow me to simply cast to AntThink on the last line of my example.

What should I do? What am I doing wrong?

Thanks!

I see two options:

  1. get the game object from the transform (clone.GameObject) and then get the script you need from there.

  2. Use GameObject clone = Instantiate(antt, …) as GameObject and then get the script from there.

Get the script with:

ScriptName other = gameObject.GetComponent<ScriptName>();

Instantiate() does not return Transform - it returns whatever you instantiated. Besides this, you can use GetComponent to grab whatever you want, like so:

Transform clone = Instantiate(antt, transform.position, transform.rotation) as Transform;
AntThink foo = clone.GetComponent<AntThink>();