- Home /
Affect every object in array.
Hello, What I'm trying to do is to disable the ball sprite renderer when the player grabs it but I have multiple players in an array list but when I run the game ball sprite renderer disabled only with the last element it doesn't work with other elements
public List<GrabController> grabScript = new List<GrabController>();
public SpriteRenderer spriteRenderer;
void Update()
{
for (int i = 0; i < grabScript.Count; i++)
{
if (grabScript[i].isGrabed == true)
{
spriteRenderer.enabled = false;
}
else
{
spriteRenderer.enabled = true;
}
}
}
Answer by privatecontractor · Jan 30 at 08:39 AM
Hi @hamoo7dfuad,
ok, solution could be bool returning value depending from checking values in List.... in Update():
spriteRenderer.enabled = whatShouldBeBallStatus();
Below add new bool:
bool whatShouldBeBallStatus()
{
for (int i=0; i< grabScript[i].Count;i++)
{
if (grabScript[i].isGrabed == true) return false;
}
return true;
}
Hope will do! Let me know! Also in case that someone will have similar issue remember to edit your initial question.
Sorry, I don't think I have explained my issue correctly what I wanted is to disable the ball sprite renderer when the Boolean is true but my code didn't work correctly it just took the last of the player's list, in other words, each player has a grab script when isGrabed equals to true the ball sprite renderer get disabled
Hi @hamoo7dfuad, check this now, bellive is ok. Remember to be more specific when you ask question.