C# noob question

In Javascript I can do this:

var hit : RaycastHit;

How do I do that in C#?

Thanks.

private RaycastHit hit;

Change private in ‘public’ to make the variable accessible for other classes and the Unity editor.

I’d suggest you to bookmark this page : C# Guide - .NET managed language | Microsoft Learn

It’s the microsoft documentation to c# and provides great example (even though it is really dispersive…) and it’s a great source for learning the basics of the c# language, other than a reference when you are coding.

Philip

Thanks you two.

Does anyone know why this:

void Update ()
{
	private RaycastHit hit;
	if (Physics.Raycast (transform.position, -Vector3.up, hit)) {
		groundGrad = hit.normal;
	}
}

gives me this error:

error CS1002: Expecting `;'

And why this:

private RaycastHit hit;
void Update ()
{
	if (Physics.Raycast (transform.position, -Vector3.up, hit)) {
		groundGrad = hit.normal;
	}
}

gives me these errors:

error CS1502: The best overloaded method match for `UnityEngine.Physics.Raycast(UnityEngine.Vector3, UnityEngine.Vector3, float)' has some invalid arguments
error CS1503: Argument 3: Cannot convert type `UnityEngine.RaycastHit' to `float'

It worked in Javascript, are the overloaded functions different in C#?

I believe it should be :

if (Physics.Raycast (transform.position, -Vector3.up, out hit)) ..

oxl