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:47 AM · enemygameobjectsattackc++targets

How to refer to a enemy health, from use that in some variables...

(in c++) for example...i want to player stop them auto-attack crpg style, when the opponent health goes to 0.

my conflict:

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


and this is my auto attack system

 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());
     }
 }

If I put Enemy.health im receive typical error set a instance of a object.

I want to change that to my opponent.health but this its wrong obiously...

This target system alocate in my enemy script :

void OnMouseOver() { Player.opponent = transform; }

thanks in advance to all

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 Hexer · Jul 27, 2015 at 04:39 PM 0
Share

Enemy.health means, Go to the script called Enemy and get the variable health. In this case health should be a static public int(or float).

If you have a health script that is attached to the enemy. Change the health variable in that script to "static public" Then you will be able to use that variable in another script.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by falac · Jul 28, 2015 at 04:30 AM

that's what I thought but not, if I'm doing something wrong because i tested in previous times and has not worked for me...

I get the null reference exception error in the line 24 for me that´s line is... "if (_enemy.health <= 0)"

for knowledge i use that player script.. and common script for all my players called "humans" using to enemy opponnent, and other commmon script for player opponent called "creature" and individuals scripts for an enemy calleds enemy, enemy02, enemy 03, etc.

i do that because i think i cant assign various opponets to my player... Example:

opponent.GetComponent(Creature).GetHit(damage);

and if i use the same script "Enemy" to all my enemies they cant coordinate in time.time attacks

please tell me what can i do to resolve that problem... or to do my enemy health non-static with this scripts

thanks to all

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;

 private Enemy _enemy; 

 void Awake ()
 { 
     player = this;
     health = maxHealth;
     _enemy = opponent.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;
     }
 }

}

using UnityEngine; using System.Collections;

public abstract class Creature : MonoBehaviour { public string name;

 public int damage;

 public int maxHealth;
 
 public int health;

 public float range;

 public void GetHit(int playerDamage)
 {
     health = health - playerDamage;
 }

 protected abstract void Attack();

}

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
0

Answer by TonanBora · Jul 27, 2015 at 04:57 PM

The reason your getting that error, is because your trying to access the Enemy script itself, not an instance of the Enemy script. You need an Enemy variable to store the Enemy script that you wish to access. Do not make the Health a static, as Hexer suggested, as this will cause more problems than it fixes.

The problem with making the Health static, is that all enemies will share the same exact health. So, if you damage one enemy, all enemies will be damaged, and enemies will be spawned with the damaged Health instead of max health.

So, what you need to do, is create an Enemy variable, and then somewhere in your code, set that variable to the enemy your attacking.

For example:

 private Enemy _enemy;
 

Then, where ever your setting the opponent variable, also do this:

 _enemy = opponent.GetComponent<Enemy>();

Then finally, instead of calling Enemy.Health, call _enemy.Health.

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 Hexer · Jul 27, 2015 at 05:19 PM 0
Share

He is right, I forgot about that.

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

Show only selected enemy's health bar 1 Answer

The name `target' does not exist in the current context 1 Answer

Can anyone help with me with my attack script and enemy script? 1 Answer

Melee range AI 1 Answer

Weapon and enemy health 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