Question by
CrazyLule · Jan 11, 2017 at 12:59 PM ·
getcomponentray
How dose getcomponentwork ?
I have error
Assets/RAY.cs(20,36): error CS0428: Cannot convert method group GetComponent' to non-delegate type
actorenemymanager'. Consider using parentheses to invoke the method
????
using UnityEngine; using System.Collections;
public class ray : MonoBehaviour {
void Update()
{
if (Input.GetMouseButton (0)) {
RayClick ();
}
}
void RayClick()
{
RaycastHit hit = new RaycastHit;
if(Physics.Raycast(transform.position , transform.forward , out hit)
{
actorenemymanager rc = hit.transform.GetComponent<actorenemymanager>;
if(rc != null)
rc.damage(10);
}
}
}
using UnityEngine; using System.Collections;
public class actorenemymanager : MonoBehaviour { float health = 150; void Start () {
}
// Update is called once per frame
void Update () {
if (health < 0) {
Destroy (gameObject);
}
}
public void damage(float damagerecived){
health -= damagerecived;
}
}
Comment
GetComponent requires parentheses after component. So in your case it would be like :
hit.transform.GetComponent<actorenemymanager>();
Your answer
Follow this Question
Related Questions
How dose getcomponent work 1 Answer
The raycast is allways looking to the camera. 0 Answers
Set public Image in canvas from script 0 Answers
Gui Text won't turn off 0 Answers