- Home /
How to assign a instantiated gameobject to other script of gameobject
Hi
My question is
How to assign a gameobject that was instantiated
to the inspector of another gameobject's script
without using gameobject.find
I use c# please help.
Answer by Seth-Bergman · Jan 12, 2014 at 07:36 AM
You should take a bit more time on your question, be specific so people can answer you better.. Now, if you want to avoid gameObject.Find, you'll have to store the object when you instantiate it, wherever that is. You can store a reference to the script you want to add it to, if that is different than the script which instantiates the object. (you will probably still want to use "GameObject.Find" to initialize it though). For example:
MyScript otherScript;
void Start(){
otherScript = GameObject.Find("NameOfObject").GetComponent<MyScript>();
}
void NewObject(){
GameObject newObject = Instantiate(prefab, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
otherScript.variableForObject = newObject;
}
p.s. if my (or any) answer helps you, please remember to accept it, thanks..
sorry
my English is not good
I will take more time to understand your solve, thx.