Hello Unity Community! I have tried to fix this, but I just can’t. What is the problem with this code!?!? Thanks, Calvin
using UnityEngine;
using System.Collections;
public class MeleeSystem : MonoBehaviour
{
public int TheDamage = 50;
public float Distance;
void Update()
{
if (Input.GetButtonDown ("Fire1"))
{
RaycastHit hit = RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit))
{
Distance = hit.distance;
hit.transform.SendMessage ("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
}
}
}
}
For future posts with syntax errors, please paste in a copy of the compiler error from the Console. It gives us information like what line the error is on and part of the stack trace.
Line 13, I’m not sure what you are attempting. Maybe:
RaycastHit hit = new RaycastHit();
Note that a RaycastHit is a struct and therefore does not need to be initialized, so you can just declare:
Raycast hit;
As for line 15, you need the ‘out’ keyword with your ‘hit’. So putting the code becomes:
using UnityEngine;
using System.Collections;
public class MeleeSystem : MonoBehaviour
{
public int TheDamage = 50;
public float Distance;
void Update()
{
if (Input.GetButtonDown ("Fire1"))
{
RaycastHit hit;
if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), out hit))
{
Distance = hit.distance;
hit.transform.SendMessage ("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
}
}
}
}