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 /
  • Help Room /
This question was closed Aug 09, 2018 at 09:24 PM by MT369MT for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by TheRealGrendel · Jun 18, 2016 at 12:12 AM · c#bugvariable

Variables not updating c# [SOLVED]

So I have a GameMaster class and PlayerMovement class. In PM, I have a public bool "win" which is set to False in Start(). It changes to True once the player collides with the objective. In GM, a function is called from PM that returns the status of "win".

So here's the problem; "win" always returns false in GM, even if "win" is true in PM. I need to figure out how to have "win" correctly update across my entire program.

After hours of testing, I have figured out these things:

  1. It is set to a null value when called in GM. I confirmed this by changing the bool to an int. No matter what number I set the initial value to, it always returned 0 in GM.

  2. This only happens to public functions in PM. Any non public function receives the correct status of the variable.

  3. If the check method is called in PM, it returns the correct status. If called in GM, it always returns false.

This question has been asked before here, but it was never answered. If you need me to provide my code, just ask. Thank you.

[SOLVED] If you are using a struct, you need to make the variable you need static. OR, use a class instead. A C# struct is read only when called from a function, but a class doesn't have this problem. See here for more detail: https://answers.unity.com/questions/425510/cant-modify-struct-variables-c.html

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

  • Sort: 
avatar image
1

Answer by Thajocoth · Jun 19, 2016 at 04:43 AM

I've found recently that objects that are "off" cause their scripts' Start()s to never be called. I would suggest printing there to be sure that Start() is even being called.

Also, are you sure that GM has the right pointers? It's possible that GM is creating PMs to point to that are never instantiated, while other PMs are being run and not interacting with the GM. To avoid this, I've been making such pointers public in my games, so I can set them directly in the inspector.

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 TheRealGrendel · Jun 19, 2016 at 04:54 PM 1
Share

So I checked and yes, Start() is being called, and Gm does have the right pointers. Luckily, after messing around with it a bit more, I discovered a workaround. Ins$$anonymous$$d of using a variable between two classes, I just had P$$anonymous$$ call a function from G$$anonymous$$, complete negating the need for the variable. But thank you for helping.

To anyone who has this problem down the road, I don't know if it can be solved, but try to see if you can work around it by calling a function ins$$anonymous$$d of a variable. Good luck!

avatar image Thajocoth TheRealGrendel · Jun 19, 2016 at 05:42 PM 0
Share

Well, I'm glad you figured out a workaround at least. Good luck with the rest of your project!

Follow this Question

Answers Answers and Comments

170 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 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

When my "Player" teleport it goes back to his last position 2 Answers

How to update variable based on scene 0 Answers

double output even though it should be possible C# 1 Answer

Variable containing a transform is modifying the object's transform it is referenced to. 1 Answer

How do I compare if a variable of integer type is between two values 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