SetActive won't work

I have some code that activates attack collision when certain sprites are played during the attack animation. The code works with the player character (attack hitboxes appear), but not with enemies (attack hitboxes don’t appear at all) . There are no error messages in the console. Here is the code:

public GameObject spriteObject;
public GameObject attack1Box, attack2Box, attack3Box;
public Sprite attack1SpriteHitFrame, attack2SpriteHitFrame, attack3SpriteHitFrame;

void Awake () 
	{
		navMeshAgent = GetComponent<NavMeshAgent> ();
		enemySight = GetComponent<EnemySight> ();
		enemyWalk = GetComponent<EnemyWalk> ();
		enemyState = GetComponent <EnemyState> ();
		animator = spriteObject.GetComponent<Animator> ();
	}

	void Update () 
	{
		currentSprite = spriteObject.GetComponent<SpriteRenderer> ().sprite;

		if (enemyState.currentState == EnemyState.currentStateEnum.attack)
			Attack ();

void Attack()
	{
		navMeshAgent.ResetPath ();

		if (attack1SpriteHitFrame == currentSprite) 
		{
			attack1Box.gameObject.SetActive (true);
		}
		else if (attack2SpriteHitFrame == currentSprite) 
		{
			attack2Box.gameObject.SetActive (true);
		}
		else if (attack3SpriteHitFrame == currentSprite) 
		{
			attack3Box.gameObject.SetActive (true);
		}
		else 
		{
			attack1Box.gameObject.SetActive (false);
			attack2Box.gameObject.SetActive (false);
			attack3Box.gameObject.SetActive (false);
		}
	}

Good day.

Are you sure you have all public variables assigned correctly? maybe you are swaping enemiy/player objects in the variable assign (in inspector). But, as i normally say in this cases…


For this kind of problems, you need to learn to find your problem by your own. As you can imagine, we can not try to read all scripts people posts, understand the logic and process, what all variables means, when or how you use them, and find where is the “problem”. You are the only one who can do it…

You need to debug the code while running, and check the states of all variables at the moment you see the problem, and I’m sure you will detect what is not how it was suposed to be.

If don’t know how, look for some tutorials on how to debug code while running.

Bye & good Luck!

There is no reason for not working, it is simple line of code. So you have some issue out of this line. Let’s see…

1) Check if you drag&drop all GameObjects.

2) Check if you call this method. Just add Debug.Log(“I am calling, i am fine”); under attack1Box.gameObject.SetActive (true); . And see in the inspector if there is “I am calling, i am fine”.

3) Check if somewhere else you are deactivating this game objects, from other script maybe.

Then say if you found something.

@tormentoarmagedoom
@bakir-omarov
Thank you, I’ve found the problem - my state mashine for some reason doesn’t see the “Attack” state. Attack animation plays, but without the state, it’s just that - an animation. Now I just need to find a way to activate the state.