unable to use "var" in C#

var test = 1;
var foo;

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#?

Thanks for reading this post.

var does not exist in C#
Thats a JS only thing

You define stuff in C# through

[= default value];

so for example public float foo = 5.0F; to expose it

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)

Hope that helps. :slight_smile:

Edit: Ah, beaten to it.

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… :wink:

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 :slight_smile: ).

hopefully a mono upgrade is on the horizon.

Bye, Lucas

thank you all for your fast replies!

I will not use the var keyword with C# unity scripts in the current version of Unity.

Cheers!

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.

Within a function:

var myInt = 1;
var myFloat = 1.0f;

will work, but

var myVariable;
myVariable = 1.0f;

will not.

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

I think this thread is 5 years old…

1 Like

Necroposter strikes again. This being one of the more confusing instances, since the initial posts are so outdated and flat out wrong now.

Yes, there are topics where necro-posting isn’t a problem, and topics where it is, and this is one of the latter.

–Eric

1 Like