I’m working on BurgZergArcades Hack and Slash RPG tutorial and I came across this error I couldn’t figure out:
using UnityEngine;
using System.Collections;
public class PlayerAttack : MonoBehaviour {
public GameObject target;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyUp(KeyCode.F)) {
Attack();
}
private void Attack() {
enemyHealth eh = (enemyHealth)target.GetComponent("enemyHealth");
eh.AdjustCurrentHealth(-10);
}
}
Does anyone know why I ran into this?
Your Update function is missing a ’ } '.
A tip: Make sure that the ‘eh’ variable is really valid before you try
to access it.
using UnityEngine;
using System.Collections;
public class PlayerAttack : MonoBehaviour {
public GameObject target;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyUp(KeyCode.F)) {
Attack();
} // <---- I added this one.
}
private void Attack() {
enemyHealth eh = (enemyHealth)target.GetComponent("enemyHealth");
// Make sure 'eh' is valid.
if(eh != null){
eh.AdjustCurrentHealth(-10);
}
}
}
Good luck!