- Home /
The name `target' does not exist in the current context
The error is on 41,79. I don't understand what's wrong.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PARCP : MonoBehaviour {
public List<GameObject> targets;
public float attackTimer;
public float coolDown;
// Use this for initialization
void Start(){
targets = new List<GameObject>();
attackTimer = 0;
coolDown = 0.5f;
}
// Update is called once per frame
void Update (){
if (attackTimer > 0)
attackTimer -= Time.deltaTime;
if (attackTimer < 0)
attackTimer = 0;
if (Input.GetKeyUp (KeyCode.Mouse0)) {
if (attackTimer == 0){
Attack();
attackTimer = coolDown;
}
}
}
private void Attack(){
RaycastHit hit;
if(Physics.Raycast(transform.position, transform.forward, out hit))
{
if(GetComponent<Rigidbody>().tag == "Enemy" && GetComponent<Rigidbody>() != null); {
EnemyHealth eh = (EnemyHealth)target.GetComponent ("EnemyHealth");
eh.AdjustCurrentHealth (-10);
}
}
}
}
Comment
Answer by DoTA_KAMIKADzE · Jun 11, 2015 at 01:07 AM
Well your Attack function is not going to work at all anyway, my guess is that you want something ~like that:
private void Attack()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit))
{
GameObject go = hit.collider.gameObject;
if (go.tag == "Enemy" && go.GetComponent<Rigidbody>() != null)
{
EnemyHealth eh = go.GetComponent<EnemyHealth>();
if (eh != null) eh.AdjustCurrentHealth(-10);
}
}
}
Your answer
Follow this Question
Related Questions
2d raycast enemy detection problem. 0 Answers
How to make enemies flash on hit 2 Answers
setting camera to hit.point returning null vector 1 Answer
Raycast causes all enemies to attack 1 Answer
Hit enemy life with raycast 1 Answer