1
\$\begingroup\$

I recently found out about the RangeAttribute and how you could use it to limit the values of your parameter when entering them in in the inspector window. However, I needed to use an array for randomly selecting Prefabs so they can spawn randomly at any given time. The problem is Unity lets me have an array size of zero, but I also want to limit the number of possible prefabs. I was wondering if there is a way to use RangedAttribute to limit the size of an array, and if not is there a different way to achieve this? I have extensive experience in Java and C# but recently started in Unity, so I don't know if I'm asking the right questions, so any guidance right now would be greatly appreciated. Thanks.

P.S. I would be able to attach the script to give an example if needed.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

AFAIK Range attribute won't be usable on arrays. As you can see here this attribute is limited to int and float restrictions.

However you can totally limit an array max size in the Editor by creating a custom inspector view. Here you will be able to check the content of your array, and reject any addition above a certain amount.

\$\endgroup\$
5
  • \$\begingroup\$ Awesome, that's perfect for what I want! Thanks so much! \$\endgroup\$ May 1, 2020 at 7:15
  • \$\begingroup\$ Hey @JonathanArendt, I'm glad if my answer helped you. Could you consider marking it as valid, then if someone else encounters the same question he would know this answer is the one that helped you? \$\endgroup\$
    – lvictorino
    May 1, 2020 at 7:27
  • \$\begingroup\$ Yep, gotcha! I looked at it and then had to walk away for a sec, so I forgot to do it, but I got it now for ya. Thanks so much once again! Like I said, I know C# but when it comes Unity, it's a whole knew learning curve. \$\endgroup\$ May 1, 2020 at 7:30
  • \$\begingroup\$ Thank you! You're a lifesaver! \$\endgroup\$ May 1, 2020 at 7:32
  • \$\begingroup\$ Haha you're welcome. Have fun with custom inspector views :) A lot of great stuff to do with those. \$\endgroup\$
    – lvictorino
    May 1, 2020 at 7:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .