- Home /
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);
}
}
Answer by tormentoarmagedoom · Jun 13, 2018 at 03:58 PM
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!
Answer by bakir-omarov · Jun 13, 2018 at 04:05 PM
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.
I double checked everything, wrote down the debug comands, but nothing works. Although, I think I know where the problem is at. You see, I have several AI scripts, united by a state machine script. This might be the problem.
public class EnemyState : $$anonymous$$onoBehaviour {
Animator animator;
AnimatorStateInfo currentStateInfo;
Nav$$anonymous$$eshAgent nav$$anonymous$$eshAgent;
EnemySight enemySight;
EnemyAttack enemyAttack;
public enum currentStateEnum { idle=0, walk=1, attack=2};
public currentStateEnum currentState;
public GameObject spriteObject;
SpriteRenderer currentSprite;
int currentAnimState;
int idleState = Animator.StringToHash ("Base Layer.Idle");
int walkState = Animator.StringToHash ("Base Layer.Walk");
int attack1State = Animator.StringToHash ("Base Layer.Attack 1");
int attack2State = Animator.StringToHash ("Base Layer.Attack 2");
int attack3State = Animator.StringToHash ("Base Layer.Attack 3");
int jumpState = Animator.StringToHash ("Base Layer.Jump");
int hurtState = Animator.StringToHash ("Base Layer.Hurt");
int fallState = Animator.StringToHash ("Base Layer.Fall");
void Awake ()
{
animator = spriteObject.GetComponent<Animator> ();
nav$$anonymous$$eshAgent = GetComponent<Nav$$anonymous$$eshAgent> ();
enemySight = GetComponent<EnemySight> ();
enemyAttack = GetComponent<EnemyAttack> ();
}
void Start ()
{
}
void Update ()
{
if (enemySight.playerInSight == true && enemySight.targetDistance < enemyAttack.attackRange && nav$$anonymous$$eshAgent.velocity.sqr$$anonymous$$agnitude < enemyAttack.attackStartDelay)
{
animator.SetBool ("Attack", true);
animator.SetBool ("Walk", false);
}
else if (enemySight.playerInSight == true)
{
animator.SetBool ("Attack", false);
animator.SetBool ("Walk", true);
}
else if (enemySight.playerInSight == false)
{
animator.SetBool ("Attack", false);
animator.SetBool ("Walk", false);
}
if (currentAnimState == idleState)
{
currentState = currentStateEnum.idle;
}
else if (currentAnimState == walkState)
{
currentState = currentStateEnum.walk;
}
else if (currentAnimState == attack1State)
{
currentState = currentStateEnum.attack;
}
currentStateInfo = animator.GetCurrentAnimatorStateInfo (0);
currentAnimState = currentStateInfo.fullPathHash;
}
}
Answer by VTepes · Jun 13, 2018 at 05:09 PM
@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.