- Home /
I need help fixing this code
using UnityEngine; using System.Collections;
public class PlayerAttack : MonoBehaviour {
public GameObject target;
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(Input.GetKeyUp(KeyCode.F)) {
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);
Debug.Log(direction);
if(distance < 2.5f) {
if(direction > 0) {
EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
eh.AddjustCurrentHealth(-10);
}
}
}
}
O.$$anonymous$$ no question just fix my script... great. Do you have errors, bugs issues? if so could you put it in the question?
Could you explain what the actual problem is, and edit your question to cut out all the irelevant parts? Nobody wants to read through an entire script just to find one bad line.
I'm just one step away from closing this since it's not a question, nor is it specific nor relevant for any other Unity user out there...
Improve the "question" or it will be removed. Read the FAQs and use some common sense.
This Is C# and i keep getting an error saying
"Assets/Scrips/EnemyAttack.cs(42,36): error CS1061: Type PlayerHealth' does not contain a definition for
AddjustCurrentHealth' and no extension method AddjustCurrentHealth' of type
PlayerHealth' could be found (are you missing a using directive or an assembly reference?)"
i am fairly new to this coding style and i would appreciate any help.
first of all i cant find a PlayerHealth Variable. Second of all when its says it doesnt have a definition for something it means that whatever your trying to access(in your situation AddJustCurrentHealth) on that variable(PlayerHealth), this variable(PlayerHealth) does not have that component or function.
Answer by rutter · Mar 06, 2012 at 04:40 AM
It'll be much easier to help you if you can be more specific about the problem(s) you're having.
This looks like a C# script, yes? If so, that GetComponent() call you're using needs to be formatted a little differently, like so:
target.GetComponent<EnemyHealth>();
I agree that he should be a bit more specific, but GetComponent works well the way he use it. Yes the generic version is shorter and a bit faster than the string version, but he doesn't need to change it because it's fine that way.
btw. the generic function is just a substitute for
Getcomponent(typeof(T)) as T
Your answer
Follow this Question
Related Questions
How To Make A Simple MMO? 3 Answers
Help! How to implement accurate attack in client-server RPG? 0 Answers
How Would I Create An (FPS RPG)? 1 Answer