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
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;
}
}
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:
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);
}
}