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 /
avatar image
0
Question by Harry Buck · May 06, 2010 at 11:07 PM · animationaienemyenemydamage

Trying to get my enemy attack damage to work

Hey everybody I amtrying to get my enemy damage to work. I have my player to attack the enemy, and when he attacks the enemy he should die. I dont have any animation set up for the death, I just want the enemy to be destroyed. So far I have this

// sound clips: var struckSound : AudioClip;

var health = 3;

function ApplyDamage( amount : int ) {

health -= amount; // <- (update on-screen health displays here)

if (health < 0) { Die(); } else { // <- (play "hurt" animation & sound here) //audio.PlayOneShot(struckSound);

 }

}

function Die() {

// now remove this gameObject from the scene:
Destroy(gameObject);

}

when I attack the enemy the editor stops and gives me the error the prefab you want to instantiate is null. What does this mean? I get this error everytime a run it.

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 dhendrix · May 06, 2010 at 11:13 PM 0
Share

You should click the error to see the line causing the problem, I don't see anything wrong here.

avatar image spinaljack · May 06, 2010 at 11:45 PM 0
Share

The error is likely from another part of the script

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by user-11402 (google) · Apr 11, 2011 at 03:48 AM

Here's the scripts i use i hope they can help

Here's the PlayerHealth script but it has GUI problems:

var maxHealth : int = 100; var curHealth : int = 100;

var healthBarLength : float ;

// Use this for initialization function Start () { healthBarLength = Screen.width / 2; }

// Update is called once per frame function Update () { AddjustCurrentHealth(0);

}

function OnGUI() { GUI.Box(new Rect(10, 10, healthBarLength, 20), curHealth + "/" + maxHealth); }

function AddjustCurrentHealth(adj) { curHealth += adj;

 if(curHealth &lt; 0)
     curHealth = 0;

 if (curHealth &gt; maxHealth)
     curHealth = maxHealth;

 if(maxHealth &lt; 1)
     maxHealth = 1;

 healthBarLength = (Screen.width / 2) * (maxHealth / curHealth);

}

and here's the EnemyAttack:

var target : GameObject; var attackTimer : float = 2; var coolDown : float = 2; var damage : int = 10;

// Use this for initialization function Start () { attackTimer = 0; coolDown = 2.0f;

}

// Update is called once per frame function Update () { if(attackTimer > 0) attackTimer -= Time.deltaTime;

 if (attackTimer &lt; 0)
     attackTimer = 0;

 if(attackTimer == 0){
     Attack();
     attackTimer = coolDown;
 }

} function Attack() { var distance = Vector3.Distance(target.transform.position, transform.position);

 var direction = Vector3.Dot(target.transform.position - transform.position, transform.forward);

 if(distance &lt; 3) {
     if(direction &gt; 0) {
         var go = GameObject.Find("Player");
         go.GetComponent(PlayerHealth).AddjustCurrentHealth(-damage);
     }
 }

}

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

No one has followed this question yet.

Related Questions

How do I animate my enemy randomly with time? 1 Answer

How do I make it so that a walking animation plays for the enemy AT THE SAME TIME it is moving toward the player? 0 Answers

Enemy Ragdoll help 1 Answer

How do I make it so that a walking animation plays for the enemy AT THE SAME TIME it is moving toward the player? 0 Answers

Problem with enemy AI 1 Answer


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