Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 falac · Jul 27, 2015 at 10:49 AM · aigameobjectstargetenemy aic++

how to refer to enemy health

Please here i have a serious dude... in vari enemy.health... enemy isnt a agameobject and i cant asign it i want to asign here my opponent (target) and stop my player auto attack when enemy health goes to 0. how can i do that???

void EnemyisDead() { if (enemy.health <= 0) { StopCoroutine(AutoAction()); } }

and here this the completly script

using UnityEngine; using System.Collections;

public class Player : Humans { public static Transform opponent;

 public static bool isAttacking;
 
 public static Player player;

 Animation animation;

 public AnimationClip attack;

 public float attackImpact;

 Enemy enemy;

 void Awake ()
 { 
     player = this;
     health = maxHealth;
     enemy = gameObject.GetComponent<Enemy>();
     animation = GetComponent<Animation>();
     initAnimations();
     isAttacking = false;
 }

 IEnumerator AutoAction()
 {
     yield return new WaitForSeconds(1);
     if(opponent != null && Vector3.Distance(opponent.position, transform.position) < range)
     {
         isAttacking = true;
         animation.CrossFade(attack.name);
         StartCoroutine(AutoAction());
     }
 }

 void EnemyisDead()
 {
     if (enemy.health <= 0)
     {
         StopCoroutine(AutoAction());
     }
 }

 //Initialize animations
 void initAnimations()
 {
     animation = GetComponent<Animation>();
     AnimationEvent attackEvent = new AnimationEvent();
     attackEvent.time = attackImpact;
     attackEvent.functionName = "impact";
     attack.AddEvent(attackEvent);
 }

 void impact()
 {
     opponent.GetComponent<Creature>().GetHit(damage);
 }

 void Update () 
 {
     Attack();
     EnemyisDead();
 }

 protected override void Attack ()
 {
     if(Input.GetKeyDown(KeyCode.Alpha1))

     {
         if(opponent != null && Vector3.Distance(opponent.position, transform.position) < range)
         {
             isAttacking = true;
             animation.CrossFade(attack.name);
             StartCoroutine(AutoAction());
         }
     }
     if(!animation.IsPlaying(attack.name))
     {
         isAttacking = false;
     }
 }

}

and here is the another

using UnityEngine; using System.Collections;

public abstract class Humans : MonoBehaviour { public string name;

 public int damage;
 
 public int maxHealth;
 
 public int health;
 
 public float range;

 public float attackRate = 0.0f;
 
 public void GetHit(int playerDamage)
 {
     health = health - playerDamage;
 }
 
 protected abstract void Attack();

}

thanks in advance

Comment
Add comment · Show 2
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 Nido · Jul 28, 2015 at 09:25 AM 0
Share

Is this line working?

void EnemyisDead() { if (enemy.health

Or your problem is accessing to enemy.health?

avatar image falac · Jul 29, 2015 at 11:46 AM 0
Share

the problems did to accesing to a enemy.health

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Tonks · Jul 28, 2015 at 04:34 PM

StopCoroutine can cause a Null Ref, try assigning the coroutine to an IEnumerator Variable first.

For example, at the start, declare an IEnumerator called AutoAction, and rename your coroutine to AutoActionCoroutine.

When you call the coroutine, define AutoAction = AutoActionCoroutine, then call StarCoroutine(AutoAction); (note the lack of brackets).

Then, when you stop, call StopCoroutine(AutoAction);. It's much more memory efficient, and can prevent Null Reference Exceptions that often occur when you start the coroutine 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 falac · Jul 29, 2015 at 11:45 AM 0
Share

ok so thanks to all thats work now but only with 1 enemy attach at it

i have serious problem now to do that with various enemies, Nido can you see me how can i use that for various scripts in one list example... enemy001 enemy002 enemy003 enemy 004... with this...

private Enemy[] enemy;

private void Awake() { enemy = FindObjectsOfType(typeof(Enemy)) as Enemy[]; }

the correct form of coroutine is like that so thanks that opens my $$anonymous$$d

for stop StopCoroutine("AutoAction");

for start StartCoroutine("AutoAction", 1.0f);

coroutine IEnumerator AutoAction() { yield return new WaitForSeconds(1); if(opponent != null && Vector3.Distance(opponent.position, transform.position) < range) { isAttacking = true; animation.CrossFade(attack.name); StartCoroutine("AutoAction", 1.0f); } }

avatar image Nido · Aug 04, 2015 at 07:56 AM 0
Share

With multiple enemies, having that Enemy[], inside the coroutine:

 foreach (Enemy oneEnemy in enemy)
 {
     //Do all stuff. 
 }

This will work for each enemy but one after one. For work with all them in parallel, you may launch a new coroutine for each enemy, and giving the enemy as argument for the coroutine.

 foreach (Enemy oneEnemy in enemies)
 {
     StartCoroutine(DoStuff(oneEnemy));
 }
 
 IEnumerator DoStuff(Enemy enemy)
 {
     enemy.transform.........
     ........................
 }

avatar image
0

Answer by Nido · Jul 28, 2015 at 12:43 PM

Try in enemy class:

 private int health; //assign value
 
 public int Health
 {
    get {return health;}
    set {health = value;}
 } 
 

Then:

 void EnemyisDead()
  {
      if (enemy.Health <= 0)  //"Health" instead of "health"
      {
          StopCoroutine(AutoAction());
      }
  }

If "enemy" returns null, make it public and attach it:

 public Enemy enemy;

And delete the line where you try to GetComponent();

If it's not possible to attach all enemies or any other problem, make it dynamic:

 private Enemy enemy;
 
 private void Awake()
 {
    enemy = FindObjectOfType(typeof(Enemy)) as Enemy;
 }
 
 //...or with many enemies...
 
 private Enemy[] enemy;
 
 private void Awake()
 {
    enemy = FindObjectsOfType(typeof(Enemy)) as Enemy[];
 }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

enemy AI follows exact path 2 Answers

Enemy AI Range Detection 3 Answers

Enemy AI 2D not working? ( Rigidbody.Addforce() ) 2 Answers

enemy raycast to detect player 1 Answer

Enemies should check if they are line of side to the player before attacking. 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