Error when converting to c#

Hello im trying to convert a javascript to c# i’ve tried my best and searched over the internet but still no luck, i get a lot of errors and the whole script is red, help :frowning:

public float Speed = 20;
	public float Range;

	void Start()
	{
		StartCoroutine(Wander());
	}
	void Update()
	{
		transform.position += transform.TransformDirection (Vector3.forward) * Speed * Time.deltaTime;

		if ((transform.position - wayPoint).magnitude < 3f)
		{
			StartCoroutine (Wander ());
		}
	}
	
	IEnumerator Wander ()
	{
		yield return new WaitForSeconds (0.08f);
		wayPoint = new Vector3(Random.Range(transform.position.x - Range, transform.position.x + Range), 1, Random.Range(transform.position.z - Range, transform.position.z + Range)); wayPoint.y = 1.5f;
		transform.LookAt(wayPoint);
	}

Hi there @carlqwe

A C# class must be contained within a class block unlike UnityScript where you can simply type functions and variables within a file. Without this class block C# will not parse or even compile your file. You’re also using a Vector3 variable called wayPoint which has not been defined. This will instantly flag an error within Unity and is the most likely cause of your problem. Simply defining the variable as a global member will fix this. For example:

public class ExampleClass : MonoBehaviour
{
	public float Speed = 20;
	public float Range;

	public Vector3 wayPoint;

	void Start()
	{
		StartCoroutine(Wander());
	}
	void Update()
	{
		transform.position += transform.TransformDirection (Vector3.forward) * Speed * Time.deltaTime;

		if ((transform.position - wayPoint).magnitude < 3f) 	
		{
			StartCoroutine (Wander ());
		}
	}

	IEnumerator Wander ()
	{
		yield return new WaitForSeconds (0.08f);
		wayPoint = new Vector3(Random.Range(transform.position.x - Range, transform.position.x + Range), 1, Random.Range(transform.position.z - Range, transform.position.z + Range)); wayPoint.y = 1.5f;
		transform.LookAt(wayPoint);
	}
}

I would highly recommend you study at least the basic syntax of C# before trying to convert from another language, or trying to script with it. I hope this helps.