I can’t really understand how to get GetComponent to work in this aspect.
Lets say I have Object A that instantiates little Object B’s. I would like Object A to be able to pass a Vector3 to the Object B to reduce any calculation that Object B would have to do. What would be the “cheapest” way to accomplish this?
Am I going to have to use a GameObject.Find(ObjectA) in every Object B? From what I’ve read .Find is not exactly cheap.
When you instantiate a prefab, the Instantiate method returns the copy. Also you can directly use a reference to one of your scripts on your prefab to instantiate it. Instantiate will always return the same type you passed in.
For example when you have a Rigidbody reference and use Instantiate on this reference, the whole Gameobject of that rigidbody will be cloned and Instantiate will return a reference to the cloned Rigidbody.
So if you have a script called ScriptB which is attached to the prefab you want to instantiate you can do this:
// C#
// inside ScriptA
public ScriptB prefab;
void CreateObjectB()
{
ScriptB clone = (ScriptB)Instantiate(prefab);
// Now you can set any variable on the new object.
// imagine ScriptB has a public Vector3 variable named "somePosition"
clone.somePosition = new Vector3(1,2,3);
}