Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Robomaster · Sep 04, 2012 at 08:08 PM · errorattackhealth

Health Bar Script Error

Hello so i have a attack script that make the enemy attack the player. But It seems as if the enemy isnt attacking the player. It will only attack the player if i click and drag the player from the hierarcy and drop it into the place on the script for the transform. but since the enemy is spawned from the project tab, i wont be able to click and drag the player transform onto the script in the hierarcy. So i was wondering what i could do to alter the scripts so that when it spawns from the project tab it will do damage to player.

Attack Script:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAttack1 : MonoBehaviour {
 public GameObject Player;
 public float attackTimer;
 public float coolDown;
 
 // Use this for initialization
 void Start () {
 attackTimer = 0;
 coolDown = 2.0f;
 }
 
 // Update is called once per frame
 void Update () {
 
 if(attackTimer > 0)
 attackTimer -= Time.deltaTime;
 
 if(attackTimer <0)
 attackTimer = 0;
 
 if(attackTimer == 0) {
 Attack();
 attackTimer = coolDown;
 }
 
 }
 
 
 private void Attack() {
 float distance = Vector3.Distance(target.transform.position, transform.position);
 
 Vector3 dir = (target.transform.position - transform.position).normalized;
 
 float direction = Vector3.Dot(dir, transform.forward);
 
 if(distance < 2.5f) {
 if(direction > 0) {

PlayerHealth1 ph = (PlayerHealth1)Player.GetComponent("PlayerHealth1");

ph.AdjustCurrentHealth(-10); } } }

 }

PlayerHealth Script:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerHealth1 : MonoBehaviour {
 public int maxHealth = 100;
 public int curHealth = 100;
 public int display = 100;
 
 public float healthBarLength;
 
 // Use this for initialization
 void Start () {
 healthBarLength = Screen.width / 2;
 }
 
 // Update is called once per frame
 void Update () {
 AdjustCurrentHealth(0);
 }
 ;
 void OnGUI() {
 GUI.Box(new Rect(172, 250, Screen.width / 4, 20), curHealth + "/"  + maxHealth);
 GUI.Box(new Rect(172, 255, healthBarLength / 2, 10), display + "/" + curHealth);
 }
 
 public void AdjustCurrentHealth(int adj) {
 curHealth += adj;
 
 if(curHealth < 0)
 curHealth = 0;
 
 if(curHealth > maxHealth)
 curHealth = maxHealth;
 
 if(maxHealth < 1)
 maxHealth = 1;
 
 healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth); 
 
 }
 }

Thanks in Advance

Comment
Add comment · Show 7
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 rcstarttofinish · Sep 04, 2012 at 08:22 PM 0
Share

For some reason my answers wont post?

I believe what you need to do is: 1. Do not play the game stay in edit mode. 2. Drag the enemy onto the hierarchy. 3. Drag the player onto the transform. 4. Delete the enemy in the project folder. 5. Drag the enemy back into the project folder. 6. Your done!

avatar image AlucardJay · Sep 04, 2012 at 08:24 PM 0
Share

Thanks for fixing the code =]

Regarding the stuffed code formatting after UDN, please read and vote up this question. I have sent an email but not received a reply. The code thing is really ruining this 'site (which used to be really easy to post code in) : http://answers.unity3d.com/questions/293216/is-there-any-way-to-fix-code-formatting-on-unity-a.html

But looking at just the question, it seems that the enemy needs to find the player so it can target it. For this you can use GameObject.Find in the Start function

Find : http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html

or FindWithTag : http://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindWithTag.html

avatar image Robomaster · Sep 04, 2012 at 08:26 PM 0
Share

Hey thanks but ive already done that and when i drag the enemy back into the project folder it automacticly deletes the transform attached from the hierarchy

avatar image AlucardJay · Sep 04, 2012 at 08:39 PM 0
Share

@rcstarttofinish

Please be patient if your question/reply doesn't show straight away.

As a new user, your posts and questions are held in a moderator que until it is approved and then it is displayed. When your karma rises, you'll be able to post questions, comments and answers without waiting for someone to approve it.

As you can see, I just found all your answers and approved them, please delete all the copies, thanks =]

@Robomaster , i think the explanation is messy, but the theory is sound with rcstarttofinish's answer. In the Project view select the Enemy prefab. In the Inspector you should see where public GameObject Player; is displayed. Drag-and-Drop the Player object from the Hierarchy window into this slot. Now whenever an enemy is Instantiated, it should have Player loaded in the public GameObject Player; slot =]

avatar image Robomaster · Sep 04, 2012 at 08:40 PM 0
Share

Thanks for the links they really helped i got my code working correctly now Thanks! :D and i just voted up your question

Show more comments

6 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by rcstarttofinish · Sep 04, 2012 at 08:38 PM

I believe what you need to do is: 1. Do not play the game stay in edit mode. 2. Drag the enemy onto the hierarchy. 3. Drag the player onto the transform. 4. Delete the enemy in the project folder. 5. Drag the enemy back into the project folder. 6. You're done!

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 rcstarttofinish · Sep 04, 2012 at 08:38 PM

I believe what you need to do is: 1. Do not play the game stay in edit mode. 2. Drag the enemy onto the hierarchy. 3. Drag the player onto the transform. 4. Delete the enemy in the project folder. 5. Drag the enemy back into the project folder. 6. Your done!

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 rcstarttofinish · Sep 04, 2012 at 08:38 PM

I believe what you need to do is: 1. Do not play the game stay in edit mode. 2. Drag the enemy onto the hierarchy. 3. Drag the player onto the transform. 4. Delete the enemy in the project folder. 5. Drag the enemy back into the project folder. 6. Your done!

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 rcstarttofinish · Sep 04, 2012 at 08:38 PM

I believe what you need to do is: 1. Do not play the game stay in edit mode. 2. Drag the enemy onto the hierarchy. 3. Drag the player onto the transform. 4. Delete the enemy in the project folder. 5. Drag the enemy back into the project folder. 6. Your done!

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 rcstarttofinish · Sep 04, 2012 at 08:38 PM

I believe what you need to do is: 1. Do not play the game stay in edit mode. 2. Drag the enemy onto the hierarchy. 3. Drag the player onto the transform. 4. Delete the enemy in the project folder. 5. Drag the enemy back into the project folder. 6. Your done!

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
  • 1
  • 2
  • ›

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Critical problem in importing scripts 3 Answers

Attack,Health and enemy health. 1 Answer

Melee Damage script by collision 2 Answers

Setting up health script. will not work, 1 Answer

Destroy(this) not working properly 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