Once IsAttackerInLane() evaluates to true it stays the same

I have used exactly the same code as used by Rick, but once IsAttackerInLane() evaluates to true it keeps true without switching to false as it should.

See how only “shoot pew pew” increases in the number while “sit and wait” sticks at the same number that it reached once I hit play button.

I suspect that this following code is the problem. Because once the childCount is gretater than one it will always evaluate to be true. But I am not sure because Rick didn’t face the same problem. Please help…

private bool IsAttackerInLane()
{
//myLaneSpawner = FindObjectOfType();
if (myLaneSpawner.transform.childCount <= 0)
{
return false;
}
else
{
return true;
}
}

Hi Angiras,

Welcome to our community! :slight_smile:

I’m not sure if I understood your problem correctly. The IsAttackerInLane() method is supposed to return true as long as the childCount is higher than 0 meaning as long as there is an attacker (= a child of the assigned lane game object) in the assigned lane.


See also:

Hi Nina, thanks for quick response. Yes but what I understand is childcount will be greater than 0 after single attacker(lizard) has spawned. Because every new lizard spawning is instantiated as child of assigned lane game object, and so the childcount will keep going up as new lizards keep spawning. And hence child count now will always be greater than zero and so the IsAttackerInLane () will keep on evaluating to be true. So the defender cannot switch from attack state to idle state, but at the start defender switches form idle to start because at the very start the assigned lane game object has no child, so childcount is less than or equal to zero. But now it can’t switch back from attack to idle as child count remains higher than zero. Hope this clarifies my query.

I will further simplify it with following code that I have modified to make my point, see the result in console.

private int IsAttackerInLane()
{
return myLaneSpawner.transform.childCount;
}

private void Update()
{
if (IsAttackerInLane() > 0)
{
Debug.Log("Shooting Child count = " + IsAttackerInLane());
}
else
{
Debug.Log("Idle Child count = " + IsAttackerInLane());
}
}

See once child count goes up, it keeps going up, as new children are instantiated and hence the following code used by Rick will evaluate as true after first instance of child is created

if (myLaneSpawner.transform.childCount <= 0)
{
return false;
}
else
{
return true;
}

Hi Nina, sorry for trouble. I figured out the problem. It was with the animator, my animator had transition from entry to cactus idle, and the fire() method is attached to cactus attack. Hence the new child that were instantiated were not destroyed as the fire() method never took place. Hence the child count was going up. The post can be closed now. Hope it helps someone who has made similar mistake by not properly configuring up the states in animator.

Good job on solving the problem! And thanks for sharing your solution! :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms