- Home /
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.
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);
@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?
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.
@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.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613183548im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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