Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheDevGuy · Aug 24, 2015 at 02:42 PM · c#clamprangeattribute

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 ;
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheDevGuy · Aug 24, 2015 at 04:06 PM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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;
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheDevGuy · Aug 25, 2015 at 11:45 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges