If I must iterate over many game objects in a for loop to call a function in a component, and all the objects are very different, what should the scripts use?
Would anyone know how to go about answering this question? Any help is greatly appreciated.
Comment
It will depend on what component you're looking for and what is knows about, but it'll be something like:
public GameObject[] gameObjects;
void Awake()
{
for (int k=0; k<gameObjects.Length; k++)
{
var component = gameObjects[k].GetComponent<SomeComponent>();
if (component != null)
component.SomeFunction();
}
}
So just to be clear, I will be using a GameObject in this instance?
Answer by MDLuffy1234FS · Jan 28, 2021 at 05:38 PM
@Myles23 I asked that same question and a guy on Discord told me this:
They will need to either implement the same interface or derive from the same base class ,@Myles23 I asked that same question on the unity discord and the guy answered this:
They will need to either implement the same interface or derive from the same base class
Your answer
