I’ve a quick question… There are multiple game objects with the same script (let’s say, script A) which contains a public bool. And these objects all share the same parent.
I want to check the children of this parent(with multiple children with and without script A) to see which objects contain script A, then check for a public bool in every one of them. If they all return false, then a separate script will return false as well, however if even one of them returns true, then that separate script should return true as well.
I’ve tried a couple of ways but couldn’t get it working right… If anyone out there could teach me how to do this, you’d make my day!
using UnityEngine;
public class SeparateScript : MonoBehaviour
{
public bool CheckIfAnyChildHasScriptA()
{
ScriptA[] components = GetComponentsInChildren<ScriptA>();
foreach (ScriptA scriptA in components)
{
if (scriptA.publicBool)
{
// At least one child has the bool set to true
return true;
}
}
// None of the children have the bool set to true
return false;
}
}
Hi there, sorry for the late response. Your response was somewhat close to what I had been trying, but it too gave inconsistent results (returns true/false constantly)… What I’ve come up with (maybe this’ll help someone else) is to have a common “pool” integer within the parent. Each child object returns a value of 0 or 1, for true or false. The pool collects all the numbers, and if the result is greater than 0, returns true, if not, returns false. This seems to work 100%!
But thanks again for your quick response!