Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
0
Question by tillmonkthered · Jul 25, 2019 at 04:31 PM · prefabsvariablesbuttonsprefab-instancestatic variable

Scripts/buttons getting old (prefab?) values of variables

Basic Problem: The scripts in my game are convinced that certain variables are set to certain values even though I can see in the Inspector and in the Game and Scene Views that the associated objects do not have those variable values. This has happened with ints, with Lists, with TMP texts, and so on.


In each case, the problem seems to be that while the correct values are showing up in the Game/Scene Views and in the Inspector, the scripts meant to be evaluating those values only ever get the "old"/"default"/"prefab" values of those variable. Any help would be greatly appreciated!


Details You Can Skip: To make up an example: an int might start at 0, and get gradually incremented as the game progresses. That int might be converted .ToString() and passed to a TextMesh Pro object for display. The TMP object might show the numbers increasing from 0 to 10 when you run the game. And the player might press a button when they see the number reach "10."


The problem in this hypothetical scenario is that the script triggered by that button always only thinks the value of the int variable is 0 (the value it started with/its value in the prefab), and that the value of the TMP string is "0". So, if the script compares the variable with the value it ought to be (in order to solve some puzzle, say), it always reports the wrong result.


This is often, though not always, associated with scripts triggered by buttons. This is often, though not always, associated with objects Instantiate()ed from prefabs. And I have often been able to "solve" the problem by declaring the variables in question "static."


(For example, if I declare the hypothetical int above "private static int," suddenly the button's script reports the updated values for the int. And if I make the TMP text object static, the button's script reports the updated values for the string version of the int.)


Summary I need the values reported/found/returned/accessed by the scripts to be the values shown in the game and in the Inspector. Declaring them all "static" often works, but also seems to be a kludge. There is clearly some important conceptual thing I am not understanding about how Unity and/or C# handles objects, variables, prefabs, or something. Any enlightenment you can offer would be appreciated!

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

Answer by nowayout2k · Jul 25, 2019 at 05:51 PM

It sounds like you may be reading data from a different instance than the one you are writing to. This is why a static variable would give you the correct value. Set breakpoints in your IDE and inspect the instance ID or use Debug.Log(GetInstanceID()) in the get and set functions to confirm. How are you Instantiating and referencing the prefab?

Comment
Add comment · Show 2 · 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 tillmonkthered · Jul 25, 2019 at 06:00 PM 0
Share

Thanks @nowayout2k! Typically I declare a new variable and set it equal to Instantiate(prefab). This is sometimes done inside a for() loop when I need multiple instances of the same prefab.

The instance ID idea is new to me, so I suspect it shall be helpful. Will report back.

Thanks again!

avatar image tillmonkthered tillmonkthered · Jul 27, 2019 at 06:09 PM 0
Share

Update I think I have a solution (well, two solutions), thanks to @nowayout2k's suggestion. While trying to add Debug.Log(GetInstanceID()) calls to scripts attached to all the objects that might be getting confused with each other, I discovered that while: parent object script calls T$$anonymous$$P.SetText() = fail parent object script calls (new script attached to T$$anonymous$$P object which calls) T$$anonymous$$P.SetText() = win

And that means I now have two different "solutions" for two different situations.

Solution 1 If (a) you are trying to change a standard C# variable (e.g., an int, a bool, a List, a string) in a script attached to that object, and (b) the change is registering in the Game View and Inspector, but (c) the script meant to check the change in value reports no change, then (d) declare the variable in question "static."

Solution 2 If (a) you are trying to change a standard C# variable (e.g., an int, a bool, a List, a string) of one object from a script attached to another object (e.g., trying to change a variable of a child from a script attached to its parent), and (b) the change is registering in the Game View and Inspector, but (c) the script meant to check the change in value reports no change, then (d) add a new script to the object whose variable you are trying to change, move the ChangeThisVariableTo(newValue) function to the new script, and have the other object's script call the function in the new script.

I wish I had something more insightful to offer others who find themselves in similar situations, but I'm grateful to @nowayout2k for the prompt!

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

113 People are following this question.

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

Related Questions

Can't modify BoxCollider center/size on base prefab 0 Answers

Find Source of Prefab Instance in Editor? 1 Answer

Instantiating Mesh Error - Mesh has Instance added? 1 Answer

Newly spawned objects not behaving similar to the original ones.,Newly spawned objects don't behave like the original ones. What do I do? 0 Answers

How do I reference any instance of a 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