• 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 dustyyeti · Oct 23, 2015 at 10:38 AM · instances

How to set a variable in an instantiated prefab; and retrieve it later.

Working in C#: This seems so basic, and I'm surprised that I'm having difficulty with it.

Simplifying the setup, I'd describe it as this:

A prefab button has a script MyScript, with a public variable MyInt.

When instantiating the buttons, I set MyInt with and index number.

During runtime, the script methods that handle PointerEventData, PointerEnter, PointerExit, ask for that instance's MyInt and send it to another routine as an argument.

The problem: Every time I interact with one of these button instances, the MyInt variable that each sends to my custom handlers is the same: "0", which is the value I set to the first instance.

I error checked to the console during the instatiation by coding a Debug.log echo of the MyInt value when the button is created, and it shows me that the values being stored are NOT zero.

My code is handling other things that are working fine, it's just this simple variable that is not performing. Is there a simple answer? (without putting up the code here??)

Thanks

Comment
Add comment · Show 1
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 Bonfire-Boy · Oct 23, 2015 at 10:53 AM 0
Share

One possibility is that the button that's handling the events isn't the one that you've initialised. What you're describing could happen for example if you're accidentally doing the initialisation on the prefab rather than its clone.

You could check for this kind of thing by also logging in the handlers (and outputting the object's unique ID to cross reference with the logging you've got in the initialisation).

Also, since your $$anonymous$$yInt is public you can see it in the inspector - does that confirm that the button in your instance has the correct value?

Those are the only things that spring to $$anonymous$$d for me without having seen the code or set-up.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Custom Team Colour Materials 0 Answers

Does GetInstanceID ever change on an object? 2 Answers

How to modify a variable on some instances of a prefab in an array of instances? 1 Answer

Performance impact of keeping all instances of a game in a single script 1 Answer

how to save instance of class trough editor 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