x


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!

more ▼

asked Jul 31 '12 at 10:43 PM

dlynch13 gravatar image

dlynch13
3 1 2 6

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jul 31 '12 at 10:46 PM

whydoidoit gravatar image

whydoidoit
33k 11 23 100

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 ++;
 }
Jul 31 '12 at 10:49 PM whydoidoit

In "teaching 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?

Jul 31 '12 at 11:06 PM dlynch13

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).

Jul 31 '12 at 11:07 PM whydoidoit

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?

Jul 31 '12 at 11:27 PM dlynch13

Its the line in dispatcher that tells it which is its owner:

    newRunner.owner = this;
Jul 31 '12 at 11:28 PM whydoidoit
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1672
x1254
x53

asked: Jul 31 '12 at 10:43 PM

Seen: 265 times

Last Updated: Aug 01 '12 at 12:42 AM