Converting MeleeSystem by Brackeys to C# (most likely a silly mistake)

I haven’t gotten used to c# syntax or error messages, so this is probably pretty simple to solve. I am trying to replicate the following code in C#:
http://s9.postimg.org/pwco3xt7z/Melee_System_Brackeys.png
(if this link breaks, the video is this

)

This is my failed attempt:

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), hit))
            {
                distance = hit.distance;
                hit.transform.SendMessage("Hurt", theDamage, SendMessageOptions.DontRequireReceiver);
            }
        }
    }
}

These are my errors:

Assets/MeleeSystem.cs(14,113): error CS0165: Use of unassigned local variable `hit'
Assets/MeleeSystem.cs(14,37): error CS1502: The best overloaded method match for `UnityEngine.Physics.Raycast(UnityEngine.Vector3, UnityEngine.Vector3, out UnityEngine.RaycastHit)' has some invalid arguments
Assets/MeleeSystem.cs(14,37): error CS1620: Argument `#3' is missing `out' modifier

If someone familiar with C# could spend a minute comparing the two programs, I would appreciate it :slight_smile:

Notice the added ‘out’

if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit))
1 Like

Put “out” in front of hit in the raycast.

1 Like

Thank you! I looked at it for ages, I can’t believe I didn’t spot that. :smile:
Sorry for the late reply, I didn’t realize that created threads were not watched automatically on the unity forums.