- Home /
Can a prefab script access a script of the object that created the prefab?
Hi there, first question ever. Thanks in advance.
I've got an object that works as a trigger, this object is a 3D scroll with a box collider and two scripts: script1.cs stores 4 strings variables, script2.cs launches a prefab when OnTriggerEnter collisions with player.
The prefab that instantiates the scrip2.cs is a GUI with a script call, let’s say, script3.cs
Now, here is the question. Can I get access to the strings stored in script1.cs from the prefab script script3.cs anyhow? I need those strings to build the GUI.
Also, I'll need this script3.cs to be able to destroy the first object, the scroll, later on.
Is there any similar like Collider col.gameobject.SendMessage not for the object that collisions, but for the object that creates the prefab.
Thanks a million
I should add: Passing a reference isn't possible, since many objects are going to create this sort of prefabs.
Answer by aldonaletto · Aug 09, 2012 at 11:55 AM
You need a reference to the object that created the script3 owner. You can declare this reference variable in script3 and assign it when creating the object:
script3.cs:
// declare the reference variable: public GameObject myCreator;
void Start(){ // get script1 using the reference: script1 script = myCreator.GetComponent< script1>(); string s1 = script.firstString; // read the strings string s2 = script.secondString; ... }
// destroy its creator when needed: Destroy(myCreator); In the instantiating script (script2.cs, I suppose):
GameObject prefab; ... // get a reference to the object instantiated: GameObject myCreature = Instantiate(prefab, ...) as GameObject; // and store the creator GameObject reference in myCreator: myCreature.GetComponent< script3>().myCreator = gameObject; ...
Yes, that's a great answer.
I guess I forgot to say that more than one objects are going to instantiate the same prefab, and that's why I won’t be able to do it this way. On your code, I would only be able to assign to the public variable myCreator one object, but the truth is I want 5 different objects creating other objects from that prefab alone.
Each object creating a prefab has different strings.
Thanks though. It was a great answer.
I could come up with a different approach, but @ the beginning I thought this one the easiest.
The variable myCreator is public because you must access it from other scripts - you will not assign anything to it in the Inspector. This variable will be assigned at runtime by the creator script, and each created object has its own myCreator variable, thus there's no way to confuse creators/creatures.
Yes, you r right. I like that approach. Sorry. $$anonymous$$arking it as answered.
But you really should pass a "script3" reference ins$$anonymous$$d of a GameObject reference ;). Within script3 you can just assign "this".
@Bunny83, this seems a clever idea, but how to access the script owner GameObject reference? this.gameObject or just this? In other words: this refers to the script instance or to the owner GameObject?
- Never $$anonymous$$d: if myCreator were a script1 reference ins$$anonymous$$d of GameObject, the creator could be destroyed with Destroy(myCreator.gameObject);
Answer by tomekkie2 · Aug 09, 2012 at 11:41 AM
Yes of course it can. Either through GameObject.SendMessage" or through passing a reference of the script1 to the script3 of the instantiated gameObject upon creation.
THAN$$anonymous$$S, BUT: GameObject.Send$$anonymous$$essage, but how?
Let’s say I want to recover the first string from script1.cs; so I write on script3.cs: string s = GameObject.Send$$anonymous$$essage("setQuestionValues"); ??? That gets me nothing.
Passing a reference isn't possible, since many objects are going to create this sort of prefabs.
Thanks
Send$$anonymous$$essage works too, but you will need a reference to the creator object anyway. If the new object were a child of its creator, you could use Send$$anonymous$$essageUpwards. Since it's not, it's faster to access directly script1 using some reference to the creator.
Your answer
![](https://koobas.hobune.stream/wayback/20220613075421im_/https://answers.unity.com/themes/thub/images/avi.jpg)