Raycast Hit Distance and Local Scale error

Hi there,

Trying to resize a mesh to the appropriate size when the Raycast hits.

Script as follow:

    if (RoutineRun) {
        var dir = Boom.transform.TransformDirection (Vector3.forward);
        var hit = RaycastHit;
        Debug.DrawRay(Boom.transform.position, dir * distance, Color.blue);

        if (Physics.Raycast (Boom.transform.position, dir,distance)) {
                Debug.Log("Boom;");
        beam.localScale = new Vector3(0.2f, 0.2f, hit.distance * 2.4f);
        }
        }

}

Keep getting 'localScale' is not a member of 'UnityEngine.GameObject'. error, and also 'distance' is not a member of 'System.Type'.

Any ideas?

EDIT:

Did declare "Distance" as follow:

var distance : float = 30;

EDIT2:

var Boom : GameObject; 
var beam : GameObject;
Boom = GameObject.Find("Boom"); 
beam = GameObject.Find("beam");

Also, I just noticed http://answers.unity3d.com/questions/50077/raycast-hit-distance-and-local-scale-error is an exact duplicate. Please try to avoid duplicating questions, as this clogs up the entire system and generally isn't very nice.

Ah, okay. You're forgiven, then. :P

2 Answers

2

First of all, you need to declare distance as a variable, probably an int or a float in your case, to avoid getting that nasty System.Type error, which (to the best of my knowledge, somebody correct me if I'm wrong) is thrown when you haven't declared a variable conflicting with a system type, and the computer grabs the system variable instead.

Secondly, replace `beam.localScale` with `beam.transform.localScale` to avoid the

'localScale' is not a member of 'UnityEngine.GameObject'

error.

You didn't specify the type of hit. You assigned the classtype to the hit variable. That's why hit is not of type RaycastHit but System.Type. You have to declare your variable as follows:

var hit : RaycastHit;

Can't see more errors since that's just a snippet of your code. As long as `beam` and `Boom` are defined as GameObjects and are set to valid objects it should work.


edit

You need to pass your hit variable to the Raycast function in order to get something back.

if (Physics.Raycast (Boom.transform.position, dir, hit, distance)) {

No, you had a "=" instead of ":" that's why your variable is of type System.Type instead of RaycastHit.

Ohh, and you didn't pass your hit variable to your Raycast function. I will add that to my answer.