Newbie - Javascript to C#

I am new here and new to programming but I am trying to learn and what I am is missing is very basic so go easy please :wink:

There is a javascript example on the unity website that goes:

var speed = 5.0;

function Update () {
    transform.Rotate(0, speed*Time.deltaTime, 0);
}

and I want to do that simple script in C# but I am having trouble figuring out how to declare the variable (at least that is what I think my problem is). What line(s) should be inserted in the below c# version?

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {

	
}

public class NewBehaviourScript : MonoBehaviour {

	// Use this for initialization
	void Start () {
		var speed = 5.0;
	
	}
	
	// Update is called once per frame
	void Update () {
		
			transform.Rotate(0, speed*Time.deltaTime, 0);
		
		//transform.Rotate(0,speed*Time.deltaTime, 0);
		//public float memberVariable =0.0F;
	
	}
}

everything seems fine you just need to take out the example part

For C#, you need to declare the variable outside of the start function. As long as it’s in the class definition, it can be used in all functions. As it is now, it can only be used in the Start() function. Let me know if that helps.

My problem, I believe, is I don’t know how to declare the variable outside of the start function.

If I attach the javascript to a gameobject in Unity it works fine and the variable appears in the inspector but if I attach the above C# code I get compiler errors (I also get debug errors in monodevelop, of course, referring to variable being assigned but not used).

I am trying to work trough the a basic tutorital on the Unity website but doing it with C# as opposed to javascript. The tutorial is here:

http://docs.unity3d.com/Documentation/Manual/Scripting.html

Simply put, if I do the java script it all works and the speed variable appears in the inspector but I can’t duplicate the success using C#.

Oh, so it’s a visibility problem? You need to declare the variable as public, as follows:

public class NewBehaviourScript : MonoBehaviour {

 
     public var speed = 5.0;

    // Use this for initialization
    void Start () {
    }

    // Update is called once per frame
    void Update () {
            transform.Rotate(0, speed*Time.deltaTime, 0);
    }
}

Try that out.

for a float variable you declare like this:

public float speed = 5.0f;

if it’s a float you need to put “f” at the end of the number

That’s how I do it myself as well, but as of 3.0, technically you can use the var keyword. http://msdn.microsoft.com/en-us/library/bb383973.aspx

ah haa! Thanks!

One more little question while I have your attention:

How do you copy the code out of a thread such as this without getting the line numbers?

Just highlight it and copy it, it shouldn’t copy the line numbers.

hmmm, IE9 does copy the line numbers but Firefox doesn’t…