Reputation: 13
Again its time for the exams, so i'm here and got again a few little problems with my code.
My goal is, beeing able to switch blocks on buttonpress (works absolutely fine), and now i added a trigger which will set a boolean on false, so i cant move those blocks anymore. This works too...but only on the last set Block i want to move.
i used (as normal?):
movingBlocks = GameObject.Find("SwitchableCube").GetComponent<MovingBlocks>();
to get the access to the script i need to switch. I tried changing the variable via function and by simply changing it. Is there anything within Unity or C# that does not allow setting a single Variable at multiple Objects?
thanks for all your help
(if i missed any thread where the solution was posted, im sooo sorry, but i was really unsure what i had to cover in my search)
EDIT:
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("DeadZone"))
{
movingBlocks.canSwitch = false;
Debug.Log("Did enter");
Debug.Log (movingBlocks.canSwitch);
}
}
void OnTriggerExit()
{
movingBlocks.canSwitch = false;
Debug.Log("probably set it on true again...without any reason");
}
thats exactly the code that changes variables, right now
Upvotes: 1
Views: 223
Reputation: 650
Find only returns one object, not multiple. Try using FindObjectsOfType
MovingBlocks[] movingBlocks = FindObjectsOfType<MovingBlocks>();
This also means you need to iterate over all of them to change the canSwitch variable for all of them.
foreach (MovingBlocks m in movingBlocks)
m.canSwitch = false;
Upvotes: 2