Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
2
Question by ina · Dec 27, 2010 at 06:13 AM · instantiateprefabvariableinstanceexpose

Updating a variable on a script in an instanced object

I'm still trying to grok Unity's naming system - I apparently still can't figure out how to update the variable on a script inside an instanced object (prefab)!

Prefab Ford has script CarsInternal.js, which has an exposed variable var cars:int; - There can be N instances of Ford, as Ford is a prefab.

A single script, External.js in a single object Brands is trying to update the cars integer in Ford. How do you update that variable from External.js?

Comment
Add comment · Show 7
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 dissidently · Dec 27, 2010 at 11:34 AM 0
Share

why did someone give this question a downgrade mark? It's a VERY good question. Very useful to those of us not gifted with innate understanding and comprehension of the ways of Unity. The official documentation is a LONG way from clear about this and does not explicitly deal with this situation.

avatar image ina · Dec 27, 2010 at 06:11 PM 0
Share

it looks like someone has been stalking down my questions and downvoting them..

avatar image azzogat · Dec 27, 2010 at 07:32 PM 0
Share

Dunno' about your other questions, but I down-voted this question because it has been asked and correctly answered many many times before.

Clicking the Variable tag would of yielded at least 10 similar questions in the first page alone (one of them is even identical in wording to yours). I assume any Answers member will first search before posting yet another question that just obscures the real questions.

avatar image ina · Dec 27, 2010 at 07:46 PM 0
Share

well i usually search both stack and google before posting, and i couldn't find one that answered my question, so if you've found duplicates, you are free to post the links here

avatar image azzogat · Dec 27, 2010 at 08:14 PM 0
Share

If you would of read my comment and pressed the Variable tag button you would of immediately found a couple of correct answers. For accessing another script:

http://answers.unity3d.com/questions/26832/cant-access-prefabs-variables-once-instantiated http://answers.unity3d.com/questions/16543/get-a-variable-from-another-script-in-unity-iphone Get an array of references ( by tag): http://answers.unity3d.com/questions/2336/how-do-i-count-or-get-references-to-all-gameobjects-that-are-tagged-with-a-partic and so on.

Show more comments

1 Reply

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

Answer by azzogat · Dec 27, 2010 at 08:38 AM

See the second example from the scripting reference (linked below). Basically, create an array of all instantiated prefabs (by tag) and loop through them. Instead of what they do in the reference page, just reference the said script like:

var CarsInternalScript: CarsInternal = go.GetComponent('CarsInternal') as CarsInternal;

and access it like:

CarsInternalScript.cars = 1337;

FindGameObjectsWithTag:

http://unity3d.com/support/documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html

GetComponent:

http://unity3d.com/support/documentation/ScriptReference/Component.GetComponent.html

If you mean changing the variable of a prefab before you instantiate it, check this manual page (and especially the Instantiating rockets & explosions and Replacing a character with a ragdoll or wreck part):

http://unity3d.com/support/documentation/Manual/Instantiating%20Prefabs.html

Hope this helped.

Comment
Add comment · Show 6 · 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 ina · Dec 27, 2010 at 08:40 AM 0
Share

okay - so I notice you don't actually have a loop there...

avatar image azzogat · Dec 27, 2010 at 09:09 AM 0
Share

Check the links I provided (as I also state in my answer). Replace what they do in the loop with what I've written here.

avatar image ina · Dec 27, 2010 at 09:22 PM 0
Share

this works - but it does not seem to catch the first one, i.e., the gameobject-to-be-instantiated that is already in the scene

avatar image ina · Dec 27, 2010 at 09:23 PM 0
Share

and i guess that is my question, how do you catch all of them, and not just most of them.

avatar image azzogat · Dec 27, 2010 at 09:49 PM 0
Share

If you mean instantiating a prefab and changing one of it's variables before instantiating, take a look at the various snippets here: http://unity3d.com/support/documentation/$$anonymous$$anual/Instantiating%20Prefabs.html especially the Instantiating rockets & explosions part.

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

No one has followed this question yet.

Related Questions

How can I modify a variable on an instance of a prefab after I created it? 2 Answers

Instantianig a teleporter prefabs with destination variables 1 Answer

[Solved]Instantiating prefab from a script and destroy it from another one 2 Answers

Issues with tracking prefab instances and gameobject naming (JS) 1 Answer

What is the difference between grey and blue prefab ? 1 Answer


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