How to set a limited number of values for a public variable?

Hello,
I think this problem is between “pure javascript” and Unity implementation.

Well, I want to create a variable in javascript called “speed”, and I want to expose it in Unity property editor.
So I wrote, in my javascript code:

var speed:int;

The problem I do not want to assign a value (e.g. 100 km/h), but a name: “LOW”, “MEDIUM”, “HI”.
So I want to see those options in the property editor :slight_smile:
Is it possible? How?

Thank you for your help (I’m still a beginner in Unity)!

Use enums:

enum Speed {Low, Medium, High}
var speed = Speed.Low;

–Eric

Like in C!

Thank you!

Can you use enums in the inspector? Is there a way to select from Low, Medium, High?

Because when I try it, it simply allows my to enter a number.

Yes. Make sure you don’t have the Inspector set to Debug mode.

Or maybe you have this problem! I did a couple days ago! Never saw it before that, though.

It’s a little buggy but super easy to fix.

Special note:

If one wants to access to “enum” defined in a script, that enum is like a “public static” variable. It means you can simply recall it without specifying the script name.

I don’t find that to be true. Do you have an example where it is?

Yes, here it is…

I made a script called TInfoWaypoint.js, and I inserted:

enum enumSuggestedSpeed {Low, Medium, High}

I applied this script to an object in the scene (a GameObject I use as a waypoint).

Now I have another object (a ship), and I use the following code in a script attached to this ship:

function getSpeedBasedOnWaypoint() {
	
	// Valore di ritorno
	var ret:float;
	
	// Divido la pMaxSpeed/3, ed ottengo "Low".
	var tmpSpeedUnit:float = pMaxSpeed / 3.0;
	
	if(nextWaypoint.wpSuggestedSpeed == enumSuggestedSpeed.Low) {
		ret = tmpSpeedUnit;
	} else if(nextWaypoint.wpSuggestedSpeed == enumSuggestedSpeed.Medium) {
		ret = tmpSpeedUnit * 2;
	} else if(nextWaypoint.wpSuggestedSpeed == enumSuggestedSpeed.High) {
		ret = pMaxSpeed;
	}
	
	return ret;
}

As you can see, I used enumSuggestedSpeed.Low
without any explicit reference to TInfoWaypoint.js or its instance.

The object are even different.

I can successfully compile and use that function.

I did not try to put enum in a code not instanced (not attached to a GameObject).

Maybe it’s a benefit of JavaScript then. That kind of thing has not been working for me.

Are you working on C#?

Can you send me a piece of code (I don’t know C# so much, but I know several languages, included Java (something similar to C#)?

Here’s the relevant part of one script.

using UnityEngine;

public class FBX_Sync : MonoBehaviour {

[System.Serializable]
public class ColliderSync
{
	public Type type;
	
	public enum Type
	{
		BoxCollider, 
		SphereCollider,
		CapsuleCollider
	}
}


}

In another script, I am forced to refer to the ColliderSync enumeration like this:

FBX_Sync.ColliderSync.Type.BoxCollider

I think the problem is “enum” is inside a class (I did not include “enum” in a class).

Did you try to refer to classname as instance or classname as Object (FBX_Sync.Type.BoxCollider) like a static variable?

Another hint: try to define enum in a Javascript file (only enums) and try to use them in C#.

If it works we could ask to Unity experts what kind of code is produced when I put javascript code in the script without class, functions, etc…