both of the above cases fail in a C# unity script file. It gives me this error:
Assets/NewBehaviourScript.cs(5,5): error CS0246: The type or namespace name `var' could not be found. Are you missing a using directive or an assembly reference? (Filename: Assets/NewBehaviourScript.cs Line: 5)
this is an example C# script that has the var keyword causing errors:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
var foo= 5.0; //fails
// Use this for initialization
void Start () {
var bar; //fails
}
// Update is called once per frame
void Update () {
var moo = 5; //fails
}
}
I am trying to follow the tutorial that uses a javascript to expose a variable to the inspector, but with using C# instead.
I know that C# 2.0 does not have the var keyword but C# 3.0 does.
Does unity not use C# 3.0? Am I doing something incorrect? Can I expose a variable to the inspector with C#?
I donât know if it is possible to use the âvarâ keyword for variable declaration in C#.
If you want to create a new variable you use:
type variable = value;
examples:
int myInteger = 10;
float myFloat = 1.124;
MyClass myInstance = new MyClass();
To control acces to your class members (variables declared within the class body) you use the keywords public, protected and private:
public int myInt = 10; (Accessible from outside the class, and also visible in the inspector)
protected int myInt = 10; (Accessible from within this class and all subclasses)
private int myInt = 10; (Only accessible from within this this particular class)
harmless, Unity currently only supports C# 2.0, so youâll have to use strong typing.
dreamora, thatâs not strictly true. var is very much available in C# 3.0. Which is all the more reason for Unity to upgrade Mono to the latest versionâŚ
Untill Unity upgrades the version of Mono they embed, you cannot use:
type inference (var a = new Bookayahsa(); )
automatic properties (public int Health { get; set; } )
lambdaâs (a => a.Length > 4)
collection initializers (a = new List() { 3, 4}; }
other than that you should be fine.
type inference and automatic properties you can actually hack in by replacing unityâs gmcs. (The resulting CIL code unity can deal with just fine). I did this for a while, but recently reverted, because the mdb files generated turned by the new gmcs.exe and unityâs gmcs.exe turned out to be incompatible, resulting in not getting linenumbers in stacktraces, which is pretty much killing, since you also have no debugger, you really need the linenumbers. (at least I did ).
public class MusicManager : MonoBehaviour
{
public AudioClip NewMusic; //Pick an audio track to play.
void Awake ()
{
var go = GameObject.Find(âGame Musicâ); //Finds the game object called Game Music, if it goes by a different name, change this.
go.audio.clip = NewMusic; //Replaces the old audio with the new one set in the inspector.
go.audio.Play(); //Plays the audio.
}
}
this works as long as u use var inside a function no worries how ever if you use it outside function call than it doesnt work
Java666 hit it on the head. Unityâs version of C# allows scoped var declaration it just has to be cast as part of the declaration and that doesnât work with parameters since they are compiled and will never know what theyâre supposed to be.
var in c# work only inside the method ,
it cannot be declared as public like other
it cannot be showed in inspector
it must be assigned eg .var test = âhiâ;
var test2 = 1;
once it is assigned we cannot change its datatype without typecast