Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 roamcel · Aug 16, 2011 at 03:47 PM · gameobjectcomponentclonessharing

Sharing components amongst different gameobjects

Easy question, but pretty difficult to figure out the answer despite modest unity knowledge.

I have a scene with a few 'master gameobjects', each with the same script that holds game state variables, but with different values.

 master1GO <= variable 1 = "a", variable 2 = "b", variable 3 = "c"
 master2GO <= variable 1 = "d", variable 2 = "e", variable 3 = "f"
 master3GO <= variable 1 = "g", variable 2 = "h", variable 3 = "i"

Then spontaneously, these gameobjects will get duplicated by the scene, up to several hundred times, with their transforms roughly randomized for scattering the things clones around.

The problem is, each instance will hold its very own game state, with its own script instance, while my problem is that I need a -single- script behind all the 'clone' instances of the same script, since I'd like to change only the "original" gamestate in master1GO, for example, and thus alter the same values in all "master1GOclones", and this is why I can't however use a static reference, since I have a random number of masterGOs with the same script of course.

Is there a way to achieve such result? I basically need to instantiate a clone of an existing gameobject, and then to substitute the cloned object script of one specific class with the original script's reference of the same class.

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
1
Best Answer

Answer by Waz · Aug 17, 2011 at 01:09 PM

Put the variables in a class, and reuse that:

 // MyScript.js
 class Variables { var v1 : int; var v2 : int; ...}
 var variables = new Variables();

 // In copying script
 var clone = Instantiate(master1);
 var script = clone.GetComponent.<MyScript>();
 var original = master1.GetComponent.<MyScript>();
 script.variables = original.variables;

Changing variables.v1 in the master will now be changing it in all copies.

Comment
Add comment · Show 3 · 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 Julien-Lynge · Aug 17, 2011 at 02:44 PM 1
Share

$$anonymous$$akes sense - you're passing by reference rather than value.

One issue that I wonder about is whether this adds significant overhead in terms of time. He stated that he 'spontaneously' adds several hundred of these, which I take to mean in a frame or several frames. I've found that just instantiating hundreds of game objects can take up to several seconds, but now we're adding getComponent calls into the mix. How quickly does Unity look up components? The actual variable assignment, if it's simple types, would be fairly non-noticeable, but roamcel, are you going to include any arrays or other large data sets in your script?

avatar image roamcel · Aug 17, 2011 at 04:00 PM 0
Share

thanks for the incredibly straightforward solution.

Regarding data size, NOAA, I'll be actually 'moving' two lists and an array of 15some values each, but I'm not worried about 'spawn time' since spawning is not time critical, fortunately, lol. Items will be actually 'faded' into scene.

avatar image Waz · Aug 17, 2011 at 11:41 PM 1
Share

@NOAA_Julie has a good point, though the Instantiate will far outweigh the GetComponent calls. If it's a problem, I'd advise spreading out the spawning over a few frames. i.e. ins$$anonymous$$d of 200 at once, do 10 per frame for 20 frames (about 0.3 seconds, so it'll be "instant" to the user).

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Does Destroy() destroy all components? 2 Answers

Clearing a LineRenderer Whiteboard, so that you can start drawing again 1 Answer

Execute Code When Component Added 6 Answers

GameObject stutters when moving if refresh rate is not set to 60hz. 1 Answer

Exposing Components to different scripts,Proper way to expose components to different scipts 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