Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by VTepes · Jun 13, 2018 at 03:53 PM · objectattackactiveactivateactivation

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);
         }
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VTepes · Jun 13, 2018 at 04:28 PM 0
Share

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;
     }
 }
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

92 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to activate another Object with java script? 4 Answers

Random time activate/deactivate object 1 Answer

How Do I deActivate One Sphere ? 1 Answer

Prefab to save active status of sub Object 1 Answer

issue with PlayerPref destroying all objects 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges