It says that the line containing: private void Attack() {, could only contain types and namespace declarations. Please help me!
using UnityEngine;
using System.Collections;
public class PlayerAttack : MonoBehaviour {
public GameObject target;
public float attackIimer;
public float coolDown;
// Use this for initialization
void Start () {
attackIimer = 0;
coolDown = 2.0f;
}
// Update is called once per frame
void Update () {
if (attackIimer > 0)
attackIimer -= Time.deltaTime;
if (attackIimer < 0)
attackIimer = 0;
if (Input.GetKeyUp (KeyCode.F)) {
if (attackIimer = 0)
Attack();
attackIimer = 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.AdjustCurrentHealth (-10);
}
}
}
For future posts, please format your code. After pasting, select your code, and use the 101/010 button. I did it for you this time.
As for your issue, you close the class on 29, so the Attack() function is being declared outside the class. The fix is to move the ‘}’ at line 29 to line 47. In addition, it should be ‘==’ on line 23. Note if you place your cursor on a ‘{’ in Monodevelop, the matching ‘}’ will be highlighted.
using UnityEngine;
using System.Collections;
public class PlayerAttack : MonoBehaviour {
public GameObject target;
public float attackIimer;
public float coolDown;
void Start () {
attackIimer = 0;
coolDown = 2.0f;
}
// Update is called once per frame
void Update () {
if (attackIimer > 0)
attackIimer -= Time.deltaTime;
if (attackIimer < 0)
attackIimer = 0;
if (Input.GetKeyUp (KeyCode.F)) {
if (attackIimer == 0)
Attack();
attackIimer = 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.AdjustCurrentHealth (-10);
}
}
}
}