- Home /
Set RangeAttribute to specific numbers
Hello,
Currently i have found out about RangeAttribute. Now this will work great, but there is one drawback unfortunately.
I would like to set the range to be specific numbers like: 0, 2, 4, 8
So there is a total of 4 values that the range can be.
Is there any way to conquer this for a public variable? Having the slider be either 0, 2, 4, 8?
Or should i be using something different? I just want to constrict the integer to those values.
Thanks in advance! Also using c#!
[Range(1,4)]
public int antiAliasingAmount ;
Also, would i use an Array instad, since there i can specify any values i want directly, and then call them in an if individually?
Answer by stepan-stulov · Aug 24, 2015 at 07:31 PM
If your field is conceptually only a limited set of values with strict borders, I would strongly recommend to use what's specifically designed for that purpose — enums. Also you can also back enums by integers and then cast. It's not as "cool" as a slider, but does the job.
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
public enum Antialiasing
{
Zero = 0,
Two = 2,
Four = 4,
Eight = 8
}
[SerializeField] private Antialiasing _antialiasing;
}
Great, that was it. I got my script working.
Now, here is my working script, i just had a small question for something i encountered.
using UnityEngine;
using System.Collections;
public class EnumScript : $$anonymous$$onoBehaviour
{
public enum AntiAliasing{NoAA, TwoAA, FourAA, EightAA};
void Start ()
{
AntiAliasing _AntiAliasing;
}
void OnValidate ()
{
if (_AntiAliasing == AntiAliasing.NoAA)
{
Debug.Log("AntiAliasing = " + _AntiAliasing);
}
else if (_AntiAliasing == AntiAliasing.TwoAA)
{
Debug.Log("AntiAliasing = " + _AntiAliasing);
}
else if (_AntiAliasing == AntiAliasing.FourAA)
{
Debug.Log("AntiAliasing = " + _AntiAliasing);
}
else if (_AntiAliasing == AntiAliasing.EightAA)
{
Debug.Log("AntiAliasing = " + _AntiAliasing);
}
}
[SerializeField] private AntiAliasing _AntiAliasing;
}
I got an error when i didnt set "AntiAliasing _AntiAliasing;" on start, is that because i have to declare that at the start in order to use it in the rest of the script?
Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220612001225im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Is there a list of variable attributes for C#? 2 Answers
How to make Audio "Range" Further. 2 Answers
How to calculate projectile range 2 Answers