• 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 DanyWind · Jul 27, 2020 at 01:54 PM · prefab-instance

All my enemies are attacking simultaneously. I wanted each spawned prefab enemy to act independently. Please Help!

public class MovementLeft : MonoBehaviour {

 float dirX;

 [SerializeField]

 Rigidbody2D rb;

 public static bool isAttacking = false;

 Animator anim;

 float speed = 1f;
 Vector3 localScale;

 
 void Start ()
 {
     localScale = transform.localScale;
     rb = GetComponent<Rigidbody2D> ();
     dirX = 1f;
     anim = GetComponent<Animator> ();
 }
 void Update()
 {
     
     if (transform.position.x < -9f)
         dirX = 1f;
     else if (transform.position.x >9f)
         dirX = 1f;
     if (isAttacking)
         anim.SetBool ("isAttacking", true);
     else
     {
             anim.SetBool ("isAttacking", false);
     }
     
 }
 void FixedUpdate()
 {
     if (!isAttacking)
         rb.velocity = new Vector2(dirX * speed, rb.velocity.y);
     else
     {
         rb.velocity = Vector2.zero;
     }
         
 }

}

public class AttackKnight : MonoBehaviour

 {

     public MovementLeft movementleft;
    
     void OnTriggerEnter2D(Collider2D col)
     {
         if (col.gameObject.name.Equals("KnightRight"))
         {
             movementleft.isAttacking = true;
         }
     }
 
     void OnTriggerExit2D(Collider2D col)
     {
         if (col.gameObject.name.Equals("KnightRight"))
         {
             movementleft.isAttacking = 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

2 Replies

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

Answer by Llama_w_2Ls · Jul 27, 2020 at 04:50 PM

When movementleft.isAttacking is equal to true, it plays an attack animation, by setting a bool in the animator to true. However, this animation is linked to all of your enemies, causing them to all play the same animation clip at the same time, im guessing. A way to solve this is by getting the name or identity of the enemy that is near the player, for example, and getting the animator component of that animation and only setting the bool to true for that specific enemy. Let me know if this is the case. I dont fully understand the context of your scripts and animator components etc.

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 jamesvhyde · Jul 27, 2020 at 06:06 PM 1
Share

Right. No need to make the bool variable static. That is making all the instances use the same value.

avatar image
0

Answer by DanyWind · Jul 27, 2020 at 06:45 PM

Thank you for your response. What I wanted is, even if you have 10 enemies close to the KnightRight they would be doing their own attacking animation and doing damage separately. you would click on a button carrying a prefab, which I already have, and one enemy would appear and get close to KnightRight and attack, then, if you click on the button again, another enemy would appear and get close to KnightRight and attack. If I remove the static I get a NullReferenceException: Object reference not set to an instance of an object on the movementleft.isAttacking = true. Thank you again.

Comment
Add comment · Show 2 · 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 DanyWind · Jul 27, 2020 at 06:53 PM 0
Share

Problem fixed. Thank you.

avatar image jamesvhyde · Jul 27, 2020 at 07:22 PM 0
Share

This means you have not set the value for movementLeft variable. If these two behaviors are on different objects, you set this in the Inspector window of the editor. Drag the object with the $$anonymous$$ovementLeft component onto the movementLeft variable in the inspector.

If the behaviors are on the same object, set the value in the Start() method of AttackKnight using GetComponent.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

133 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 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

Why can't I undo changes to a prefab? (or to an instance of it after I apply the changes) 1 Answer

Unity 2D Game Issue: Script is only affecting one prefab instead of all of them. 0 Answers

Client unable to retain reference to prefab 0 Answers

How to instantiate prefabs exactly in the way as drag'n'drop does? 1 Answer

Adding a child via code in edit mode won't work for prefab instance 0 Answers


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