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 /
  • Help Room /
avatar image
0
Question by FreezKid · Sep 15, 2015 at 08:40 AM · enemy healthenemydamageenemys

duplicating enemies not working correctly

Hai i have an enemy, its working good, but when i create duplicates of the enemy, only the original can attack me. I can't attack the duplicate, but if i attack a duplicate the original dies and duplicates stands there with default animation. My health variable is not static. here are my codes

Enemy_movement

 public static Enemy_movement Instance;

 Transform player;
 NavMeshAgent nav;

 // Use this for initialization
 void Awake () 
 {
     Instance = this;
     player = GameObject.FindGameObjectWithTag ("Player").transform;
     nav = GetComponent<NavMeshAgent> ();
 
 }
 
 // Update is called once per frame
 void Update () 
 {

     if (Enemy_Health.Instance.CurrentHealth > 0 && Player_Health.Instance.CurrentHealth > 0)
     {
         nav.SetDestination (player.position);
     }
     else
     {
         nav.enabled = false;
     }
 
 }

Enemy_Attack

   public static Enemy_Attack Instance;

 public float TimeBetweenAttack = 0.5f;
 public float distance;
 public int AttackDamage = 10;
 public float[] AnimLength = new float[3] {0.458f, 0.500f, 0.500f};
 
 public GameObject targetLookAt;
 GameObject player;

 Animator anim;
 bool inAttackRange;
 //Enemy_Health enemyHealth;
 float timer = 0f;
 int attackCount=0;

 // Use this for initialization
 void Awake () 
 {
     Instance = this;
     player = GameObject.FindGameObjectWithTag ("Player");
     anim = GetComponent<Animator> ();    
     targetLookAt = GameObject.Find ("targetLookAt") as GameObject;
 }
 void Update () 
 {
     timer -= Time.deltaTime;
     distance = Vector3.Distance(targetLookAt.transform.position, gameObject.transform.position);
     Debug.Log ("Distance=" + distance);
     if (distance <= 2.5f) 
     {
         inAttackRange = true;
     }
     else if (distance < 10f) 
     {
         inAttackRange = false;
         anim.Play("attack_walk");
         anim.SetBool ("InRange", true);
         attackCount = 0;
         timer = 0f;
     } 
     else 
     {
         anim.SetBool ("InRange", false);
     }

     if (Player_Health.Instance.CurrentHealth <= 0) {
         anim.SetTrigger ("PlayerDead");
     } else 
     {
         if (inAttackRange&& Enemy_Health.Instance.CurrentHealth > 0) 
         {
             Attack ();            
         }
     }
 }

 void Attack()
 {
     if (timer <= 0f) 
     {
         if (attackCount == 0) 
         {
             timer = AnimLength [0];
             timer += 0.3f;
             anim.Play("attack_1");
             attackCount++;

         }
         else if(attackCount == 1)
         {
             timer = AnimLength [1];
             timer += 0.3f;
             anim.Play("attack_2");
             attackCount++;

         }
         else if(attackCount == 2)
         {
             timer = AnimLength [2];
             timer += 0.3f;
             anim.Play("attack_3");
             attackCount++;

         }
         else if(attackCount > 2)
         {
             attackCount = 0;
         }
     }
 }

 public void DamagePlayer() //animation event function
 {
     if (Enemy_Weapon_Collision.Instance.colliding) 
     {
         if (Player_Health.Instance.CurrentHealth > 0)
             Player_Health.Instance.TakeDamage (AttackDamage);
     }

 }

Enemy_Health

 public static Enemy_Health Instance;

 public int StartingHealth = 100;
 public int CurrentHealth;
 public int ScoreValue = 10;
 private GameObject ragdoll;

 // Use this for initialization
 void Awake () 
 {
     Instance = this;
     CurrentHealth = StartingHealth;
 }
 
 // Update is called once per frame
 void Update () 
 {
 
 }

 public void TakeDamage (int amount)
 {
     CurrentHealth -= amount;

     if (CurrentHealth <= 0) 
     {
         Death();
     }
 }
 
 void Death()
 {
     if (ragdoll == null) 
     {
         ragdoll = GameObject.Instantiate(Resources.Load("Ragdoll_samurai"), 
                                          transform.position, 
                                          transform.rotation) as GameObject;
     }
     
     var characterRoot = transform.FindChild("metarig");
     var ragdollRoot = ragdoll.transform.FindChild("metarig");
     
     //Match the ragdoll's skeleton to character's skelecton
     MatchChildrensTransforms (characterRoot, ragdollRoot);
     
     //destroy the character
     Destroy(gameObject);
     Collision_check.Instance.InRange = false;
     
     //Tell the camera to look at the ragdoll
     TP_Camera.Instance.TargetLookAt = ragdoll.transform.FindChild("metarig/Root");
 }

 void MatchChildrensTransforms(Transform source, Transform target)
 {
     //Traverse through the skeleton hierarchy to matching joint rotation
     if (source.childCount > 0) 
     {
         foreach (Transform sourceTransform in source.transform)
         {
             Transform targetTransform = target.Find(sourceTransform.name);
             
             if (targetTransform != null)
             {
                 MatchChildrensTransforms(sourceTransform,targetTransform);
                 targetTransform.localPosition = sourceTransform.localPosition;
                 //targetTransform.localRotation = sourceTransform.localRotation;
             }
         }
     }
 }

Player_Health

 public static Player_Health Instance;

 public int StartingHealth = 100;
 public int CurrentHealth;
 public Slider HealthSlider;
 public Image DamageImage;
 public float FlashSpeed = 5f;
 public Color FlashColour = new Color (1f, 0f, 0f, 0.1f);

 Animator anim;
 bool isDead;
 bool damaged;


 // Use this for initialization
 void Awake () 
 {
     Instance = this;
     anim = GetComponent<Animator> ();
     CurrentHealth = StartingHealth;    
 }
 
 // Update is called once per frame
 void Update () 
 {
     if (damaged)
         DamageImage.color = FlashColour;
     else
         DamageImage.color = Color.Lerp (DamageImage.color, Color.clear, FlashSpeed * Time.deltaTime);

     damaged = false;
 
 }

 public void TakeDamage (int amount)
 {
     damaged = true;
     CurrentHealth -= amount;

     HealthSlider.value = CurrentHealth;

     if (CurrentHealth <= 0 && !isDead) 
     {
         Death();
     }
 }

 void Death()
 {
     isDead = true;
     anim.SetTrigger ("Dead");
     TP_Animator.Instance.Die ();
 }


please help, this is my project and am stuck here, thank you.

,

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

1 Reply

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

Answer by MathieuBarbier · Sep 15, 2015 at 01:31 PM

You are using a singleton pattern for your health managers. This pattern only allows one instance of a class to be used at any given time. You should remove all those "instances" variables and use another architecture if you want to use multiple players / ennemies...

Comment
Add comment · Show 3 · 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 FreezKid · Sep 15, 2015 at 03:29 PM 0
Share

So how do I do it?? can you give me an example?? do I have to redo all my codes?? this is my project and gotta submit by 28 this month, I still have some animations to do too. Please help. and thank you for your reply..... :)

avatar image MathieuBarbier FreezKid · Sep 15, 2015 at 03:35 PM 0
Share

You have to remove all these instances vars and be able to target your gameobject specifically. You have multiple ways to do that. You can use GameObject.Find for the quick and ugly way. Youcan also use gameobject references...

avatar image FreezKid MathieuBarbier · Sep 15, 2015 at 04:23 PM 0
Share

hai I did as you said and it worked, but created a new problem, now enemies are not following the character anymore, it has something to do with the player_health script. its not being accessed by gameobject.getcomponent(); because its values are not changing and its functions are not being called. one note though all my other script of the player are made using Instance, except player_health script. is that why ?? or is it possible to give an object both of these script?? my other player scripts are not accessing any value from this class. thank you for the quick reply

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

28 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

Related Questions

If one prefab enemy dies, the other enemy prefabs malfunction 0 Answers

enemy death physics 0 Answers

Enemy damage script is not working 0 Answers

Damage is done to enemy even when not attacking 0 Answers

Problem Calling Function from Other Script 2 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