- Home /
How to communicate from Prefab/Instance back to script that instantiated it?
I have read every manual, user guide, and post I can find, but I still don't understand this.
I have a "Dispatcher" and a "Runner". The Dispatcher uses Instantiate() to create a Runner prefab. That prefab needs to tell the Dispatcher when it has collided with something so that the Dispatcher can take action. How does the Runner (Instance) communicate back with the Dispatcher (Parent Script)?
Dispatcher.js (attached to Empty game object as central controller):
var runner : GameObject; // A prefab is linked in Inspector
var collisionCount = 0;
function LateUpdate () {
var newRunner = Instantiate(runner, Vector3(0,0,0), Quaternion.identity);
}
Runner.js (attached to Prefab):
function OnCollisionEnter (other:Collision)
{
// Increment Dispatcher collisionCount in Dispatcher, but how??!!!
}
What is wrong with my thinking? How else can you code an intelligent program without a central controller? I need someone to be the referee. I have seen a great demo on this, (Intergalactic Sheep Pong) but it wasn't using Instantiated Prefabs.
I also don't see how the Dispatcher can communicate with Runners either since it re-uses newRunner for every instantiation. It can only commuicate during this one LateUpdate() unless it saves a tag or something.
Thanks!
Answer by whydoidoit · Jul 31, 2012 at 10:46 PM
You are right - you need to teach the runner about the dispatcher.
Runner.js
var owner : Dispatcher;
function OnCollisionEnter(other : Collision) {
owner.collisionCount++;
}
Dispatcher.js
function LateUpdate() {
var newRunner = Instantiate(runner, Vector3.zero, Quaternion.identity);
newRunner.owner = this;
}
Note if you used owner in Awake on the Runner then you could actually modify the value in the prefab before calling instantiate.
Another option for a central controller is the Singleton pattern - in that case you do this:
Dispatcher.js
static var Current : Dispatcher;
function Awake() {
Current = this;
}
...
Runner.js
function OnCollisionEnter(other : Collision) {
Dispatcher.Current.collisionCount ++;
}
In "$$anonymous$$ching the runner about the dispatcher", I tried exactly what you suggested at first, but Unity would not let me drag a script onto a Prefab for the Dispatcher slot in the Inspector. The only options for that slot are primitives (cube,sphere,plane,etc.) It "feels" like a Unity bug. I am using 3.5.3f3. Is that a known issue?
You need to allocate that at run time - like the example I show here. You can't set it up that way in a prefab (the setting of scripts on assets won't work because the prefab cannot refer back to a scene object).
Excellent information: "the prefab cannot refer back to a scene object." Good to know!! So then how does Runner.js (in the first example) know what "var owner : Dispatcher;" refers to? That is a slot in the Runner prefab that Runner.js is attached. What if I had multiple objects with a Dispatcher script attached? How does owner.collisionCount know who is the owner?
Its the line in dispatcher that tells it which is its owner:
newRunner.owner = this;