- Home /
Array script boolean
Hello guys, i have prob. I have array of Transform[]. This transforms have script attached to them. I need to open door when all scripts boolean isActive = will be true; So my code is:
Update()
{
for(int i = 0; i < Activators.Length; i++)
{
activators = Activators[i].GetComponent<m_Activator>();
if(activators.isActive) // How i need to change this?
{
//DO SOMETHING.
`
` }
}
}
}
So this works if i do one activator bool isActive = true; I need to it work when i do
isActive = true for all Transforms with this scripts. What should i do? Thanks.
Answer by syclamoth · Nov 04, 2011 at 02:40 PM
You do this-
bool allActive = true;
foreach(GameObject activator in activators)
{
if(!activators.GetComponent<m_Activator>().isActive)
{
allActive = false;
}
}
if(allActive)
{
// They're all active.
}
I'm assuming activators is an array of GameObjects? Otherwise, just change the type at the start of the foreach, it should work for just about anything.
foreach(Transform activator in Activators) { if(!activator.GetComponent ().isActive){
allActive = false;
}else if(activator.GetComponent<m_Activator>().isActive)
{
allActive = true;
}
}
Hmm if i active first third activator 3 in list(it's last from list) allActive is activated
No, get rid of the else if bit- it's screwing you up. All it needs is for one of them to return false- you don't care if they return true, because true is the default state! (note how the code I posted didn't have that line) (there's a pattern there)
Use what syclamoth posted and leave out the whole else if
part. For this approach is important to not set allActive
to true
in the loop.
Oh my fault, thank you guys. I am defined allActive not in Update..at start. Thank one more time.