Putting the Reward method before or after SetActive()?

At the end of the course, we see the 2x methods getting added: RewardGold() and StealGold()

But one is placed before the SetActive() method.
And the other is set after the SetActive() method.

Is there any reason for this order?
Do they work the same way, no matter the order?

Is this

IEnumerator FollowPath()
{
[...]

enemy.StealGold();
gameObject.SetActive(false);   
}

the same as this?

IEnumerator FollowPath()
{
[...]

gameObject.SetActive(false);  
enemy.StealGold();
}

Hi,

Good catch. This is a little inconsistency in the code. I would disable the game object last because one cannot know what happens in Unity.

The best would be to test the code. If the outcome is the same, the order does not matter. In that case, the methods in the enemy object get called and executed independently from the state of our game object. However, if the outcome is not the same, it might be that gameObject.SetActive(false); terminated our coroutine.

Did this help?


See also:

It did, thanks Nina.
Both approaches work, so I guess I just keep it consistent.
I just thought that maybe, once a gameobject is no longer active, its scripts & associated methods also stop working. But that doesn’t seem to be the case. Maybe the object is no longer present in the scene, but its game logic is still running.

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

Privacy & Terms