- Home /
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
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!
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
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
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 =]
Thanks for the links they really helped i got my code working correctly now Thanks! :D and i just voted up your question
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!
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!
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!
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!
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!
Your answer
Follow this Question
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