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 /
avatar image
1
Question by Antviss · Feb 06, 2017 at 07:49 AM · c#functionvariablesordering

Variable value doesn't change

Hello everyone,

Ths has been driving me crazy for a good few hours now and after looking up and down Google I'm no closer to finding the answer to this.

I've got this very basic script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class PlayerDetails : MonoBehaviour {
     private int i;
        
     void Awake()
     {
         i = 0;
        
     }
     public void ChangeMe()
     {
         i = 2;
     }
 
    void Update()
     {
         Debug.Logg(i);
         if (i == 2)
         {
             Debug.Log(i);
           }
     }
 
 }

Very simple script as I said. Now, this may be me being daft, but the way I read this is: i is delcared (private int i) i is initialised (in Awake) ChangeMe is called from the OnClick() of a button in the scene which should change i value to 2 Update constantly shows the value of I 0 by default 2 if the value is 2

What happens in my program (new scene, new project, no other scripts but this) is:

0 is showed every frame. When the button is pressed 2 is shown ONCE, then it goes back to 0.

This is where I'm confused. Shouldn't i stay 2 after the function changes it? If not, how to I make it stay 2 in this context please?

Many thanks for your time!!

Alex

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 DavoMyan · Jun 20, 2018 at 06:13 PM 0
Share

Did you manage to fix this? I am having the exact same problem right now and its so weird, I feel like I am making a noob mistake

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Vicarian · Jun 20, 2018 at 08:29 PM

There's nothing wrong with your code other than having an extra 'g' on line 21. What unity does with messages printed to the console is that it collapses them by default. It checks the value of the output and marks a quantity for times that output has been seen. It won't print line by line unless you uncheck Collapse in the Console. You don't want to use the console to display things like this on Update() either. Best is to mark the field with [SerializeField] and just inspect the object the script is on. You'll see the value without getting spammed by Console entries. The less you use Update() the better. You can remove the [SerializeField] attribute later if you don't need to modify the value at design time.

 using UnityEngine;
  
 public class PlayerDetails : MonoBehaviour {
     [SerializeField] private int i;
         
     void Awake() {
         i = 0;
     }
     
     public void ChangeMe()
     {
         i = 2;
     }
 }



Comment
Add comment · Show 3 · 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 Alemira · Jul 30, 2019 at 04:14 PM 0
Share

This is not the right answer. The problem isn't in the console but rather in the update method that seems to not receive the "new" value" of the variable.

avatar image Vicarian Alemira · Jul 30, 2019 at 04:21 PM 0
Share

I'll concede it's not the best written. I should have made it clearer that the Log statement on line 21 is the cause of seeing all the 0s. The variable i does in fact change and remain changed, as long as the button click event is subscribed to Change$$anonymous$$e(). Log collapsing will make it appear that the variable did not change, however, with code as the original poster wrote.

avatar image Bonfire-Boy · Jul 31, 2019 at 08:26 AM 0
Share

An alternative to using SerializeField in that way is just to set the inspector to debug mode temporarily in order to view the private field. That way you don't have to temporarily modify the code.

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

11 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

Related Questions

Function is not updating the variable 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How can you hide variables within a region in a c# script? 1 Answer

Why do I get this error when enable/disable rooms? 0 Answers


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