• 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 Rookiebatman · May 25, 2019 at 12:08 AM · intsequencenumbers

Trying to figure out the best way to have a series of ints that are dynamically numbered in sequence

I want to have a set of named ints (not a list, because adding ints to a list and then changing the value of the item in the list doesn't change the original) that, when the appropriate function is triggered, will be given a sequential numbering (0, 1, 2, 3, etc). Of course, that's easy enough to just do by hand, but what I want is to be able to add new variables or remove existing ones, and still have them be sequentially numbered. So like if the set is "ExampleIntBlue, ExampleIntRed, ExampleIntYellow;" then ExampleIntBlue would be 0, ExampleIntRed would be 1, and ExampleIntYellow would be 2. Then if I added ExampleIntGreen in between red and yellow (and then triggered the numbering function again), I would want ExampleIntGreen to be 2 and ExampleIntYellow to be 3. If I then removed ExampleIntRed and ran the function again, green would be 1 and yellow would be 2.

Is there any way to do that, that would be easier than just renumbering them manually every time? (The list won't be super-long, but it might be like 20 or 30 items.) The only t$$anonymous$$ng I can t$$anonymous$$nk of is doing them as GameObjects with a string to define the name of the resulting int, and then having the parent of those objects run through the loop of all the c$$anonymous$$ldren (since doing that goes in the order they're listed in the $$anonymous$$erarchy). But I figure there's gotta be an easier way to do it than that.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by highpockets · May 25, 2019 at 01:46 PM

I t$$anonymous$$nk t$$anonymous$$s would be a good place to use the insert method of List. List.Insert(index, item); But if Count == Capacity I believe the item gets added to the end of the list. If the list always stays the same size, just remove the item that needs the index change and then insert it to the desired index.

 List.Remove(“green”);
 List.Insert(3, “green”);
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 Rookiebatman · May 25, 2019 at 02:20 PM 0
Share
avatar image
0

Answer by troien · May 25, 2019 at 03:54 PM

T$$anonymous$$s part of your question

(not a list, because adding ints to a list and then changing the value of the item in the list doesn't change the original)


Although t$$anonymous$$s is true for an integer, that has not$$anonymous$$ng to do with the list. If you place a 'reference' type item in the list (a.k.a. a class) then modifying it will change the original. If you place a 'value' type in the list (a.k.a. struct) (int, bool, float, etc. are all value types) then modifying it will only modify the value in said list. See also t$$anonymous$$s answer on stackoverflow


It doesn't matter where you store the value or reference (variable, Array, List, LinkedList, etc.) the above statement holds. Meaning the 'list' has not$$anonymous$$ng to do with the fact whether it updates the original or not, the original type decides that.


Now i'm not sure what your intent is here with these integers. And why you put then in a list in the first place, and then still want to acces them outside your list. So i'll just write a very general 'wrap it in a class' solution here so you can change the value in a list w$$anonymous$$le at the same time changing the original...

 public class Example
 {
     public int Value { get; set; }
 
     public Example(int value)
     {
         Value = value;
     }
 }

Usage of t$$anonymous$$s class would be like t$$anonymous$$s:

 List<Example> test = new List<Example>();
 
 Example test1 = new Example(0);
 Example test2 = new Example(1);
 
 test.Add(test1);
 test.Add(test2);
 
 test[0].Value = 10;
 Debug.Log(test1.Value); // Will return 10 instead of 0
Comment
Add comment · Show 6 · 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 Rookiebatman · May 25, 2019 at 04:37 PM 0
Share
avatar image troien Rookiebatman · May 25, 2019 at 04:56 PM 1
Share
avatar image Rookiebatman troien · May 25, 2019 at 05:16 PM 0
Share
Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

108 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sequential GameObject Activation 2 Answers

Periodic fraction gets converted to int 1 Answer

help in unity! please enter! 0 Answers

How to make an int not pass 0 2 Answers

How to make a random occurrences using a timer? 3 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