Static properties in UnityScript?

For various reasons outside of my control I have to use UnityScript rather than C#. I have gotten non-static properties to work fine in UnityScript but not static ones.

I used to have this code:

public static var currentType : int = 0;

But then I wanted a hook to when this value is changed, so I tried this:

private static var _currentType : int = 0;
public static function get currentType() : int { return _currentType; }
public static function set currentType(value : int) {
    _currentType = value;
    Debug.Log("Selected type: " + _currentType); 
}

But when I compile and some object tries to call ClassName.currentType I get this error:

Anyone know why this doesn’t work?

Well, UnityScript has not been updated recently as far as I can tell. You should really switch to C# or JavaScript.

@Eric5h5 might have a clue. My UnityScript is too weak. It looks how I would expect a static property to appear.

If you need a quick hack you could always wrap it in an instance member. Its dodgy, but it should work.

Unity doesn’t use Javascript and never has. The choices are Unityscript, and C#. Neither have been updated in years, so telling people to switch doesn’t help anyone.

Anyway, as far as I know properties were never fixed to support static types. You’d have to use standard functions instead; that’s what properties basically do anyway.

–Eric

Actually, If you go into the Unity Documentation, the only options for your code type are C# and JScript. there is no option for UnityScript in the docs. And Unity does use JavaScript. C# HAS been updated numerous times, I know. I have been using C# in Unity for 4 years and it has updated a few times. If it has never been updated, how can we use the newest C# methods if the version you say Unity has does not support them?

We can’t use the newest C# methods. o.O There are so many things not available in Unity’s C# like dynamic or async / await, null propagation, etc.

1 Like

Pipe down before you hurt yourself.

UnityScript is the community’s name for the language Unity calls JavaScript. They are the same thing. UnityScript is preferred because it makes it clear that the language is not ECMA JavaScript that you would use in web development.

It’s also true that niether language has been actively upgraded by Unity for several years. Unity’s version of C# is about equivalent to 3.5. Considering the language is up to 6.0 now, that’s a fair few versions behind.

C# in Unity hasn’t been updated since somewhere around Unity 2, so you’re quite mistaken. It’s way behind the current C# used in .NET. If you’d ever written actual Javascript code, you’d know that Unity doesn’t use Javascript, no matter what they might choose to call it.

Anyway this is all completely off-topic.

–Eric

1 Like

I have written in Java script. I started with it with Unity. If it is not supported, why is it an option?

It’s not. They called it “Javascript”, probably for marketing reasons, but Unityscript is not Javascript. You don’t think simple Unityscript code like “var foo : int = 5” would work in Javascript, I hope. Calling C# “Java” wouldn’t somehow magically turn C# into Java, either. But like I said, this is all off-topic, so further messages along these lines in this topic will be deleted. You can start another topic if you want to discuss this further.

–Eric

2 Likes