There is no variable for hit? pls help me

I am trying to convert this javascript into c# and so far it’s going pretty good but i can’t fix this last part because it keeps saying this:
Error CS1503 Argument 3: cannot convert from ‘UnityEngine.RaycastHit’ to ‘float’
Error CS0119 ‘treeController’ is a type, which is not valid in the given context

this is the javascript

#pragma strict

var rayLength = 10;

private var treeScript : TreeController;

function Update()
{
    var hit : RaycastHit;
    var fwd = transform.TransformDirection(Vector3.forward);
   
    if(Physics.Raycast(transform.position, fwd, hit, rayLength))
    {
        if(hit.collider.gameObject.tag == "Tree")
        {
            treeScript = GameObject.Find(hit.collider.gameObject.name).GetComponent(TreeController);
           
            if(Input.GetButtonDown("Fire1"))
            {
                treeScript.treeHealth -= 1;
            }
        }
    }
}

and this is my converted c# script that has some problems:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class rayCastTree : MonoBehaviour {
    public float rayLenght = 10;
    public treeController treeScript;
    public RaycastHit hit;
    public Transform fwd;

    // Use this for initialization
    void Start () {
       
    }
   
    // Update is called once per frame
    void Update () {
        int rayLength = 10;
        var fwd = transform.TransformDirection(Vector3.forward);
        //var hit = ?????????????
        if (Physics.Raycast(transform.position, fwd, hit, rayLength))
        {
            if (hit.collider.gameObject.tag == "Tree")
            {
                treeScript = GameObject.Find(hit.collider.gameObject.name).GetComponent(treeController);

                if (Input.GetButtonDown("Fire1"))
                {
                    treeScript.treeHealth -= 1;
                }
            }
        }
    }
}

Maybe try -

if (Physics.Raycast(transform.position, fwd, out hit, rayLength))
1 Like

Thx that fixed one of my problems but i still have my problem that my treeController is a type which is not valid in the given context… but thx tho

treeScript = GameObject.Find(hit.collider.gameObject.name).GetComponent();

Thank you thats all fixed now but my last problem is that i have those options right now and the only options i need is ray lenght the rest are things that i dont need how do i remove them?

3269292--252457--d09a68ff097087555a51a9a86d2c7fe2.png

what do you mean by remove them? in the RayCastTree script you can delete the variable or you can set them to private so that they don’t show in Unity. inspector.
hope this helps

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rayCastTree : MonoBehaviour {
    public float rayLenght = 10;  //this is misspelled that may have been one of your issues.  it's currently not getting used.
    private treeController treeScript;  //this is need, the getcomponent is storing it here,  i've set it to private so it's not useable from outside this script.
    //public RaycastHit hit;  //don't need this
    //public Transform fwd;  //you're creating this in the update.  you don't need it.

    void Start ()
    {
     
    }
 

    void Update ()
    {
        int rayLength = 10; // you created this becouse of the misspelled above?
        var fwd = transform.TransformDirection(Vector3.forward); //you're creating this every frame

        if (Physics.Raycast(transform.position, fwd, out hit, rayLength))
        {
            if (hit.collider.gameObject.tag == "Tree")
            {
                treeScript = GameObject.Find(hit.collider.gameObject.name).GetComponent<treeController>();
                if (Input.GetButtonDown("Fire1"))
                {
                    treeScript.treeHealth -= 1;
                }
            }
        }
    }
}
1 Like