Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dlynch13 · Jul 31, 2012 at 10:43 PM · instantiateprefabcommunication

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Jul 31, 2012 at 10:49 PM 0
Share

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 ++;
  }
avatar image dlynch13 · Jul 31, 2012 at 11:06 PM 0
Share

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?

avatar image whydoidoit · Jul 31, 2012 at 11:07 PM 0
Share

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

avatar image dlynch13 · Jul 31, 2012 at 11:27 PM 0
Share

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?

avatar image whydoidoit · Jul 31, 2012 at 11:28 PM 0
Share

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

     newRunner.owner = this;
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing LocalScale in code affecting prefab stored in Assets 1 Answer

Instantiate a prefab vs create one dynamically at runtime? 1 Answer

Spawning a prefab at another object's location 3 Answers

Instantiate individual components of a prefab? 1 Answer

Issue Instantiating prefab in C# 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges