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 jurgjurg · Feb 17, 2015 at 09:52 AM · variableupdateupdate function

variable not updating inside void Update()

this is my code:

 public void Update () {
 
         Debug.Log (currentPlayerIndex.ToString ());
         players [currentPlayerIndex].TurnUpdate ();
     
     }
 
     public void NextTurn(){
 
 
         Debug.Log ("STARTcurrentplayer: player"+currentPlayerIndex.ToString ());
     
 
         if (currentPlayerIndex < amountPlayers -1) {
 
 
             currentPlayerIndex+=1;
 
                         
                 } else {
 
             currentPlayerIndex = 0;
 
         }
 
         if (currentPlayerIndex == userPlayerIndex) {
                     
             CanvasScript.Canvasinstance.showCanvas();
                     
                 }
 
 
 
         Debug.Log ("ENDcurrentplayer: player"+currentPlayerIndex.ToString ());
 
         }

I fire the nextturn() from a UI button, that all goes well. the Code inside nextturn also seems to work according to the debug logs which fire correctly.

In my update() on the otherhand the Debug.Log(currentPlayerIndex.ToString()) keeps giving 0, it never increases. and the turnupdate from players 0 keeps firing and the turnupdate of player 1 never fires.

These turnupdates just consist of a debug.log with debugs a message like 'heu' or 'hoi'.

amount of players = 2 //is a public int set in the inspector

userplayerindex = 0

//I do know i can use ++ instead of +=1, but i think im going to use this form anyway later.

Comment
Add comment · Show 9
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 chariot · Feb 17, 2015 at 10:07 AM 0
Share
 if (currentPlayerIndex < amountPlayers -1) {
  
  
              currentPlayerIndex+=1;
  
                          
                  } else {
  
              currentPlayerIndex = currentPlayerIndex;
  
          }

Also u dont need to convert to string in Debug.Log, just use Debug.Log(currentPlayerIndex);

avatar image Bonfire-Boy · Feb 17, 2015 at 10:59 AM 0
Share

@chariot Can you explain what you think the issue is and why that would fix it? (I think you're missing the fact that the reset to zero is there to make currentPlayerIndex cycle).

@jurgjurg I'm curious to know how you know that amountPlayers =2. Is there a reason not to use players.Length ins$$anonymous$$d? So long as you have no nulls in the area, it would be more reliable (wouldn't depend on your maintaining the amountPlayers variable)

And what does any of this have to do with the Start() function?

avatar image Nymisu · Feb 17, 2015 at 11:12 AM 0
Share

Debug.Log doesn't need to be told explicitly to convert something into a string, by the way. It can take just about any infotype you throw at it.

The only plausible reason it cannot run is because your amountplayers is not set properly.

Furthermore, we really can't see any working code past that complete guess.

Also to make the code much more readable, try this:

  public void NextTurn()
 {
     Debug.Log ("STARTcurrentplayer:player"+currentPlayerIndex);
     if (currentPlayerIndex < amountPlayers -1) currentPlayerIndex+=1;
     else currentPlayerIndex = 0;
 
     if (currentPlayerIndex == userPlayerIndex) CanvasScript.Canvasinstance.showCanvas();
 
     Debug.Log ("ENDcurrentplayer: player"+currentPlayerIndex);
 }

Unless your "if" or "else" contains more than one line, it's good practice to leave the brackets out. Saves lines, writes faster, is easier to read.

Finally, no matter how simple and straightforward you think your code is, it's good practice to comment it. Not because someone else might read it, but because you'll forget what it exactly does and how in a week.

avatar image Bonfire-Boy · Feb 17, 2015 at 11:44 AM 0
Share

@Nymisu and others....

Things like use of brackets and ToString() are totally a matter of style and taste.

Personally, I prefer to use brackets always (makes it more readable and less prone to error), and to use ToString() when my variable is not a string (highlights the fact that it's not a string and therefore a formatting function is being used implicitly.

I have no problem with people preferring to do it a different way, and neither should you!

Totally agree about the commenting though, and all the more so when sharing code and asking people to help with it.

avatar image Nymisu · Feb 17, 2015 at 12:35 PM 0
Share

I'd like to argue that using .toString() isn't a matter of taste, as you're likely forcing a conversion twice when it isn't necessary. Unity editor's console might display chararrays ins$$anonymous$$d of strings, for instance, and forcing .toString() method on it ins$$anonymous$$d of letting it do the conversion itself can cause performance hits during debugging.

As for how brackets go, entirely a matter of taste, i agree. However admittedly as i'm a fan of compact code, and straying away from extra characters, i had somewhat of a struggle reading your's, but i can see some benefits in it.

Show more comments

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.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

Variable goes very high in update method 1 Answer

How to change the rate of decrease in a variable in the update function 1 Answer

Play method only ONCE when detected by the update function 3 Answers

Why a public varibale is not accsible inside Update() ? 1 Answer

How do I update an image to all connected Players on a Multiplayer server? 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