• 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 nicoolsen10 · Aug 09, 2019 at 07:28 PM · enemydie

When more than 1 enemy they cant die

So I'm trying to make an enemy but when there are 2 enemies when the first one dies the second one just go in minus in health and I just can't seem to fix it can anyone help me?

 using System.Collections.Generic;
 using UnityEngine;
 using TMPro;
 using System.Collections;
 
 public class StoneGolemController : MonoBehaviour
 {
     #region Variable Declarations
 
     GameObject Player;
 
     public List<GameObject> Targets;
     public GameObject Orb;
 
     public TextMeshProUGUI displayText;
 
     public float MoveSpeed = 2f;
 
     float searchRadius = 100f;
     float meleeRadius = 2f;
     float throwRadius = 5f;
     public float maxThrowingStones = 30f;
     public float currentThrowingStones;
     public Transform ThrowingStone;
 
     float currentTargetDistance;
 
     public static StoneGolemController instance;
     public float MaxHealth = 500;
     public float CurrentHealth;
     public static int DeathHealth = 0;
 
     public GameObject StoneGolem;
 
     public bool onCooldown = false;
     public float delay = 3f;
 
     GameObject myTarget;
     #endregion
 
     void Awake()
     {
         Player = GameObject.FindGameObjectWithTag("Player");
         Orb = GameObject.FindGameObjectWithTag("Orb");
         StoneGolem = GameObject.FindGameObjectWithTag("StoneGolem");
         instance = t$$anonymous$$s;
         CurrentHealth = MaxHealth;
 
         Targets = new List<GameObject>(GameObject.FindGameObjectsWithTag("Building"))
         {
            Orb
         };
 
         //ThrowingStone.transform.position = StoneGolem.transform.position;
 
         currentThrowingStones = maxThrowingStones;
     }
 
     void Update()
     {
         {
             TargetFinder();
 
             if (myTarget == null)
                 currentTargetDistance = searchRadius;
             else
                 Move(myTarget.transform);
             {
                 if (currentThrowingStones > 0)
                 {
                     if (currentTargetDistance <= throwRadius && !onCooldown)
                     {
                         StartCoroutine(ThrowDelayLogic());
                         MoveSpeed = 0f;
                     }
                 }
                 if (currentThrowingStones > 0)
                 {
                     if (currentTargetDistance >= throwRadius && !onCooldown)
                     {
                         MoveSpeed = 2f;
                     }
                 }
                 if (currentThrowingStones <= 0)
                 {
                     MoveSpeed = 2f;
                     if (currentTargetDistance <= meleeRadius)
                     {
                         DoMelee();
                     }
                 }
             }
 
             if (CurrentHealth <= DeathHealth)
             {
                 print("Enemy Has died!!!");
                 Destroy(StoneGolem);
             }
         }
 
         void TargetFinder()
         {
             foreach (var x in Targets)
             {
                 float distance = (transform.position - x.transform.position).magnitude;
 
                 if (distance < currentTargetDistance)
                 {
                     myTarget = x;
                     //displayText.text = "My target: " + x + "\n Distance to Target: " + distance; //skal fjernes nå det er helt klart
                     currentTargetDistance = distance;
                 }
             }
         }
 
         void Move(Transform t)
         {
             transform.LookAt(t);
             transform.position += transform.forward * MoveSpeed * Time.deltaTime;
         }
 
         void DoMelee()
         {
             //if ($$anonymous$$t.collider.tag == "Player")
             {
                 //PlayerController eHealth = $$anonymous$$t.collider.GetComponent<PlayerController>();
                 //PlayerController.instance.CurrentHealth -= Random.Range(minWeaponDamage, maxWeaponDamage + 1);
                 //print("Player took some damage");
             }
             myTarget = null;
             currentTargetDistance = searchRadius;
         }
 
         void DoThrow()
         { 
             myTarget = null;
             currentTargetDistance = searchRadius;
             Instantiate(ThrowingStone);
             currentThrowingStones--;
         }
 
         IEnumerator ThrowDelayLogic()
         {
             DoThrow();
             onCooldown = true;
             yield return new WaitForSeconds(delay);
             onCooldown = false;
         }
     }
 }
Comment
Add comment · Show 1
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 sacredgeometry · Aug 09, 2019 at 07:31 PM 0
Share

public static StoneGolemController instance;

Whats this?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sacredgeometry · Aug 09, 2019 at 07:33 PM

The problem is probably:

 public static StoneGolemController instance;

The static modifier means that its a class member not an instance member. So there is only ever one of them. Assuming that you are creating a bunch of these it will be the last one you created. If you are referencing it elsewhere then you will always be referencing that last StoneGolemController.

See: StaticMember Documentation

Comment
Add comment · Show 5 · 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 nicoolsen10 · Aug 09, 2019 at 07:38 PM 0
Share

But if i remove it this wont work how do you wanna fix it?

    private void DoAttack()
     {
 
         Ray ray = PlayingCam.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
 
         if (Physics.Raycast(ray, out hit, myWeapon.attackRange))
         {
             if (hit.collider.tag == "StoneGolem")
             {
                 StoneGolemController eHealth = hit.collider.GetComponent<StoneGolemController>();
                 StoneGolemController.instance.CurrentHealth -= Random.Range(minWeaponDamage, maxWeaponDamage + 1);
                 print("Enemy took some damage");
             }
         }
     }
avatar image sacredgeometry nicoolsen10 · Aug 09, 2019 at 07:46 PM 0
Share

It doesn't work because your code is not correct.

So you need to choose a method for referencing your object. If you are using RayCasting you can get the GameObject/ Component from the Collider that gets hit.

Let me mock up an example for you

avatar image sacredgeometry nicoolsen10 · Aug 09, 2019 at 07:56 PM 0
Share
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player : MonoBehaviour
 {
     void Update ()
     {
         if(Input.GetKeyDown(KeyCode.Space)) 
         {
             DoAttack();
         }
     }
 
     private void DoAttack()
      {
          Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
          RaycastHit hit;
  
          if (Physics.Raycast(ray, out hit, Mathf.Infinity))
          {
              if (hit.collider.tag == "Enemy")
              {
                 var enemy = hit.collider.GetComponent<EnemyScript>();
                 enemy.Health -= 25;
 
                 print($"Enemy took some damage. Health = {enemy.Health}");
              }
          }
      }
 }
  
 public class EnemyScript : MonoBehaviour
 {
     public double Health = 100;
 }
 
avatar image sacredgeometry nicoolsen10 · Aug 09, 2019 at 07:56 PM 0
Share

Is the example clear enough or do you need me to make a video explaining it?

Edit: There. thats as little code as you need.

avatar image nicoolsen10 sacredgeometry · Aug 09, 2019 at 08:03 PM 0
Share

It could be nice if you could make a video cause i cant get it to work

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

110 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

Related Questions

Unity Engine GameObject error? help please 1 Answer

Kill the Player? 1 Answer

Mario first person game, help? 1 Answer

Asset spit fire but is no longer working, but when i check the backups it works as intended. 0 Answers

Force enemy to fire at a specific point, == appears not to work. 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