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 oneWaveStudios · Apr 15, 2012 at 11:01 PM · instantiatevariableongui

Variables in Instantiated Prefab Scripts and OnGui

Hi, I learned a lot from Unity Answers and spent my whole weekend searching for a usable one as I don't like posting ones myself. As I did not find anything suitable I do have to make that step:

My problem is that I have a prefab with a script as component. I Instantiate that prefab and while instantiating I change the variable of that script to a certain value

 var Script = SomeArray[SomeInt].GetComponent(SpecificScript);
 Script.CurID = GameSettings.BuildingID;

Now the instantiated object script uses that CurID variable as wished. The problem I get is that the OnGUI() function in the prefab only uses the CurID of the first instantiated object. Means if I use a menu of the second instantiated object the OnGUI() of this function uses the CurID of 1. But when I pause the game and look in the inspector, the CurID variable of the cloned object is 2 as it should be.

What am I doing wrong? Or is this a known problem and is there a workaround?

I hope you got a sense of what my problem is, it is a little bit complicated thank you in advance for your help!

Comment
Add comment · Show 3
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 Atrius · Apr 16, 2012 at 02:06 AM 0
Share

The script that has the OnGUI function and the SpecificScript are both components of the same prefab? Also what data type is the array? GameObjects?

avatar image IgnoranceIsBliss · Apr 16, 2012 at 02:56 AM 1
Share

Do you have two objects with the same SpecificScript comoponent? It's possible that they are both trying to display their GUI at the same time.

$$anonymous$$ake sure you aren't using static variables in your SpecificScript class that may be being shared between all instances.

And finally, put a 'Debug.Log(CurID);' in your OnGUI code to double-check the value then look in the console when you hit Play. You may find something important.

avatar image oneWaveStudios · Apr 16, 2012 at 10:47 PM 0
Share

@ Atrius: I use the onGUI() of the instantiated prefab and the Array is a GameObject Array, yes.

@ IgnorancelsBliss: The Gui is displayed by asking for a static variable, so you mean that everytime I click on any instantiated prefab I only call the $$anonymous$$enu of the first prefab and that's why I don't have the desired CurID? That could be the solution, so I better put the OnGUI part into another script I guess...oh boy that's going to be complicated...

Thank you for your answers, I will try outsourcing the function tomorrow and then report back if this is solved!

1 Reply

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

Answer by oneWaveStudios · Apr 24, 2012 at 02:17 PM

IgnorancelsBliss made the right suggestion, I used the GUI component with a static variable, so everytime an object was clicked it would show up the GUI of the first object, no matter what is number was, thank you very much! --@admin: Solved

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

Change prefab before instantiation 1 Answer

Updating a variable on a script in an instanced object 1 Answer

Trouble setting a variable to a game object using instantiate. 1 Answer

Instantiate JS error that i can't figure out 1 Answer

Instantiate new object from scratch 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