- Home /
Question by
$$anonymous$$ · Feb 19, 2015 at 07:35 PM ·
arrayvariablecheck
Check variable in each object of array
I have an array of objects that the play can climb on. What i want to do is make it so on the player Climbing_player = true if variable climbing = true in any of the objects of the array. However i dnt know how to check if a variable is true in each one.
This is the code on the player checking if the varable is true.
var body : Rigidbody2D;
var climb_objects : GameObject[];
var climbing_player : boolean = false;
function Start ()
{
climb_objects = GameObject.FindGameObjectsWithTag("Climb_obj");
}
function Update ()
{
for (var Climbing in climb_objects)
{
if(Climbing)
climbing_player = true;
}
if(climbing)
{
body.gravityScale = 0;
}
}
Comment
Best Answer
Answer by hexagonius · Feb 19, 2015 at 07:49 PM
Just a guess, but the bool you want to check is in a script attached to your "Climb_obj" GameObjects, right?
for (var Climbing in climb_objects)
{
if(Climbing.GetComponent<TheScriptWithTheBool>().thebool)
climbing_player = true;
}
Answer by Kiwasi · Feb 19, 2015 at 08:21 PM
In C# this is done with foreach. As in
foreach (GameObject climbGameObject in climb_objects){
if(climbGameObject.GetComponent<Climbing>().isClimbing){
climbing_player = true;
}
}