- Home /
How to make the Boolean equal to true for only one object
Hi, I have a grab controller that works perfectly but I got multiple players and I have a boolean switch to true when the player grabs it but I have a bug that when two or more players get to the ball and grab it at the same the boolean of all the players will be true but what I wanted is the boolean switch to true for only one player I have tried to fix it but it didn't work.
if (grabSc[0].isGrabed == true && grabSc[1].isGrabed == true)
{
grabSc[0].isGrabed = true;
grabSc[1].isGrabed = false;
Debug.Log("bothAreGrabed");
}
Answer by wndlnd · Feb 06 at 11:59 AM
How about replacing isGrabed with grabedBy and storing the player or the PlayerID in the grabedBy variable.
In addition, you can always create an isGrabbed getter property:
public bool isGrabed => grabbedBy != null;
public Player grabbedBy { get; private set; }
public bool TryGrab (Player player)
{
if (isGrabed)
return false;
grabbedBy = player;
return true;
}
Your answer
Follow this Question
Related Questions
question about array or list of boolean 1 Answer
Check to see if any bool instance is true? 1 Answer
Can´t instantiate objects in list correctly 1 Answer
Pathfinding through pairs of connections 2 Answers
How to put gameObjects to the list? 4 Answers