- Home /
.getComponent() creates an Instance?
Hi,
I am trying to acess an function from a script which is attached to an Gameobject. There is no problem with that.
Here is the call:
m_goTurtleSpawner.GetComponent<TurtleSpawner().getTurtleForDamage();
This function gets called. I am looping through a for-loop within this function. There is my problem. The size of the array is always 0.
for (int i = 0; i < m_lTurtles.Count; i++)
{
...Some Stuff...
}
I am logging the size of the array everytime an "enemy" spawns and the numbers are correct.
So my question is if there is the posibility that the getComponent() function creates a new reference? Then it would be clear, that the array is empty.
Thanks :)
Answer by Bunny83 · Jan 03, 2012 at 10:21 PM
No, GetComponent will not create anything. It can only return components that already exists. If there isn't such a component it will return "null".
You posted two very small code-snippets which aren't really connected. I guess that your getTurtleForDamage function returns the m_lTurtles array, so you should search for the problem there.
Thank you for your answer. :) I found the problem! It was in fact a problem of reference, but not in the way I thought of.
I was initializing the Gameobject m_goTurtleSpawner with a public variable. I throw the prefab into the Gameobject and so it creates an Instance and doesnt use the "right" spawner from the scene.
This simple line in the Start() function was the error :)
m_goTurtleSpawner = GameObject.FindGameObjectWithTag("Spawner");