How do get multiple variables from different objects to test if they are true?
I am trying to set up a turn based game where I move my team then the computer moves theirs, and I want to detect when all of them are done moving so that I can switch the turn over. I have this in all of the scripts I want to check.
public class PlayerScript : MonoBehaviour
public bool
HasNoMovement;
public float
RemainingMovement;
void Update()
{
if (RemainingMovement == 0)
HasNoMovement = true;
}
From here on out is where everything kinda falls apart for me, on another script I have
public class TurnManagerScript : Monobehaviour{
private bool
PlayerTurn;
private GameObject[]
Players;
private int
PlayerNumber;
void Update()
{
if (Players[PlayerNumber].GetComponent<PlayerScript>().HasNoMovement == true)
PlayerNumber++;
if (PlayerNumber > Players.Length)
EndingThePlayerTurn();
}
}
which doesn't work, and will not fire the EndingThePlayerTurn method. So my question is how do I get this second code to work?
Hello! Please tell me what exactly doesn't work in your second script? As I can see, there is no errors in this code. $$anonymous$$ay PlayerNumber is not correct? Lets check: please insert Debug.Log("Player number = " + PlayerNumber) before the line if (PlayerNumber > Players.Length) and see the console.
It's very possible that it's 0 based indexing (on accident?) so your PlayerNumber will only ever equal the Players.Length - 1. You could try using
if(PlayerNumber == Players.Length)
But as @Denvery said, logging is the best place to start since you don't know what values you are getting.
It is spitting out zero, It doesn't appear to be incrementing it, I will try to see if there are other references to it.
The only other reference is in the EndingThePlayerTurn method, which resets it to zero.
Nvm, it was the zero Indexing. I forgot to save before I started playing which led to the all zero's. thank you for helping me!
Answer by Sheckerol · Aug 12, 2016 at 06:48 PM
After putting in the script @Denvery told me I found that I had made the mistake @Aeleck pointed out. I forgot that Players.Length was one higher than the index. I thank these two for helping me figure it out. It now looks like this public class TurnManagerScript : MonoBehaviour
private bool
PlayerTurn;
Private GameObject[]
Players;
private int
PlayerNumber;
void Update()
{
if (Players[PlayerNumber].GetComponent<PlayerScript>().HasNoMovement == true)
PlayerNumber++;
If (PlayerNumber == Players.Length)
EndingThePlayerTurn();
Your answer
![](https://koobas.hobune.stream/wayback/20220612090656im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to run the same script multiple times in code but with different instances of it 0 Answers
Instantiate prefabs at different position 2 Answers
Unable to effect movement on active game object 0 Answers
Accessing the array of a null item / Moving items in array 0 Answers
Grid-based 2D game (turn based): How should I get started? 1 Answer