• 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
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 thing I can think 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 children (since doing that goes in the order they're listed in the hierarchy). But I figure there's gotta be an easier way to do it than that.

Comment

People who like this

0 Show 0
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

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

I think this 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

People who like this

0 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

I don't think I can use a List at all, though, because I want to change the original variables. I want other scripts to be able to refer back to ExampleIntGreen (based on the value that green dynamically has at any given moment through this method), not ExampleList[3]. If you add ExampleIntGreen to a list, it just copies the current value of that int and doesn't make any further changes to the original.

avatar image

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

This 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 this is true for an integer, that has nothing 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 this 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 nothing 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 while at the same time changing the original...

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

Usage of this class would be like this:

 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

People who like this

0 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

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.

My intent is to use this in a turn-based strategy game, where the ints represent different individual phases of a turn. It's not even really about changing the values of the ints during the game, it's really just a matter of having them be fluid during the development and prototyping stage. When I'm scripting, I have different actions and functions restricted to their specific phase, and I have a central int keeping track of what phase it currently is.

So like, if I script the function for a move action, I only want that to trigger during the movement phase. That's easy enough, I just wrap it inside a "if (CurrentPhaseInt == MovementPhaseInt)" statement. The problem is, during the prototyping stage, I don't really know for sure how many phases there will be, or what order they'll be in. Let's just toss out a hypothetical turn sequence for illustrative purposes...

ActivationPhaseInt = 1;

MovementPhaseInt = 2;

SelectCombatPhaseInt = 3;

ResolveCombatPhaseInt = 4;

CombatAdvancePhaseInt = 5;

ReinforcementPhaseInt = 6;

ResupplyPhaseInt = 7;

RegroupPhaseInt = 8;

...Those aren't really the phases in my game, but you get the idea. Now, let's say that I decide to move the Reinforcement Phase to right after the Activation Phase. I have to renumber all the ints from 2 to 8. If I then take out the Activation Phase completely, I have to renumber all the values that are left. Not difficult, by any means; just tedious, and I'm hoping there's a better way. It might not seem like much, but the longer the list gets, the more annoying it is to ever move anything around, and that puts a damper on creativity.

I think part of my problem is that before I learned to code, I got accustomed to using Excel to do a lot of things that I might have done with programming if I knew how (like generating random numbers). But Excel is also just really good at storing data in an orderly fashion, so I think there are a lot of situations where I think, "this is really easy to do in Excel, so it should be easy to do in code too." In Excel, if you Cut a cell in Row 10, then Paste it between the cells in Row 5 and 6, then it just moves the other cells down and takes up a position in Row 6. That's essentially the idea of what I'm trying to do here, be able to rearrange elements and have them renumbered dynamically.

avatar image troien Rookiebatman · May 25, 2019 at 04:56 PM 1
Share

It sounds like you are describing an enum actually :p So i think that is what you are looking for... (This site explains them pretty well I think) So for you example, you'd create a new script, call it Phase, delete content and replace it with this:

 public enum Phase
 {
     ActivationPhase,
     MovementPhase,
     SelectCombatPhase,
     ResolveCombatPhase,
     CombatAdvancePhase,
     ReinforcementPhase,
     ResupplyPhase,
     RegroupPhase
 }

Now in the rest of your code, you can use it like this:

 Phase currentPhase = Phase.ActivationPhase;
 
 if (currentPhase == Phase.RegroupPhase)
 {
     // Do something
 }

By default, the first one gets index 0, second one gets index 1, etc. So reordering them automatically changes index, but you can number them yourself if you want (just set = 5 behind a name and before the comma for example). You can also cast them to int to use the greater/lower then operators etc.

avatar image Rookiebatman troien · May 25, 2019 at 05:16 PM 0
Share

That sounds like it might be exactly what I'm looking for. I'll definitely give that a try, though it will take some effort to reformat the existing script from the int stuff it already has.

Do the enum values work just like ints in every other respect? Like, if I did "Phase currentPhase = 0" then did "currentPhase++" would that turn currentPhase to 1 (without adjusting the value of any other items in the set)?

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