Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 8r3nd4n · Jun 22, 2014 at 07:28 AM · arraylisticonsiterate

Displaying/getting elements of a list in a certain order

I have some powerups for my game that I have stored in a list.

Each powerup lasts for a certain duration.

When a powerup is received, it is added to the list as below:

  `bonusQueue.Add(BonusType.SLOWMO);`

  `bonusQueue.Add(BonusType.BOUNCY);`

etc...

Likewise when the timer for each powerup is finished, I remove the powerup from the list like so:

  `bonusQueue.Remove (BonusType.SLOWMO);`

At most there can be 5 powerups in the list and it changes dynamically accordingly.

The powerups all work as I want but I am having trouble with the icons I want to display for the powerups.

I want to display all powerups stacked according to the order they were received like the picture below:

alt text

So as a powerup is added to the list, it should go to the start of the list. As such the icons will work by icon 1 will display whatever is at list element 1, icon 2 will display list element 2, etc...

If there are no elements present at a certain index, it will display no icon.

I have some code and comments in psuedocode

 public GameObject powerupIcon1;
 public GameObject powerupIcon2;
 public GameObject powerupIcon3;

 public void ChangeIcons()
 {
     for(int i = 0; i < internalManager.bonusQueue.Count+1; i++)
     {
             if(internalManager.GetBonus(i) == InternalBonusManager.BonusType.NULL) 
             break;
          Debug.Log (System.Enum.GetNames(typeof(InternalBonusManager.BonusType))[i]);
             //get the name of the powerup at position [i]
             //if the name at [1] == "SLOWMO", powerupIcon1.Sprite.name == "SlowMo";
             //if the name at [1] == "BOUNCY", powerupIcon1.Sprite.name == "Bouncy";
             //if the name at [2] == "SLOWMO", powerupIcon2.Sprite.name == "SlowMo";
             //and so on until number 5
     }
  }

 public BonusType GetBonus(int index) {
         if(bonusQueue.Count == 0) return BonusType.NULL;
         if(index >= bonusQueue.Count) return BonusType.NULL;
         return bonusQueue[index];
     


My main problem I think is not fully understanding how to properly work a list and how to iterate through it to find a certain value.

I may be wrong in assuming that when a new element is added to the list, it goes to the front and that when an element is removed, the other all shift down in position.

Any help or ideas for how to achieve this greatly appreciated.

icons.jpg (19.9 kB)
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

3 Replies

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

Answer by 8r3nd4n · Jun 22, 2014 at 12:36 PM

I managed to get it going using a switch case and looping through the list getting each element and assigning that element to the icon:

 public class BonusManager : MonoBehaviour {

     public UILabel[] powerupLabel;            //Our Powerup text label
     public GameObject[] powerupIcon;        //Our Powerup Icon
     public InternalBonusManager internalManager = new InternalBonusManager();
   
 public void ChangeIcons()
     {
         // update the interface for the (up to) three other weapons
         
         for(int i = 0; i <= internalManager.bonusQueue.Count; ++i) 
         {
             if(internalManager.GetBonus(i) == InternalBonusManager.BonusType.NULL)
             {
                 powerupIcon[i].GetComponent<UISprite>().spriteName = "Empty";
             }
             
             switch(internalManager.GetBonus(i))
             {
             case InternalBonusManager.BonusType.BOUNCY:
                 powerupLabel[i].text = internalManager.BouncyDuration.ToString("##");
                 powerupIcon[i].GetComponent<UISprite>().spriteName = "Bouncy";
                 break;
                 
             case InternalBonusManager.BonusType.SLOWMO:
                 powerupLabel[i].text = internalManager.SlowMoDuration.ToString("##");
                 powerupIcon[i].GetComponent<UISprite>().spriteName = "SlowMo";
                 break;
                 
             case InternalBonusManager.BonusType.STRAIGHT:
                 powerupLabel[i].text = internalManager.StraightShooterDuration.ToString("##");
                 powerupIcon[i].GetComponent<UISprite>().spriteName = "StraightShooter";
                 break;
             }
         }
 
     }
 }


  public class InternalBonusManager {
 
     public float BouncyDuration { get; set; } // rapid's duration
     public float StraightShooterDuration { get; set; } // rapid's duration
     public float SlowMoDuration { get; set; } // rapid's duration
 
     // the list of weapons
     public enum BonusType {
         NULL, BOUNCY, STRAIGHT, SLOWMO
     }
 
     public List<BonusType> bonusQueue = new List<BonusType>();


 public BonusType GetBonus(int index) {
         if(bonusQueue.Count == 0) return BonusType.NULL;
         if(index >= bonusQueue.Count) return BonusType.NULL;
         return bonusQueue[index];
     }
 }

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

Answer by zee_ola05 · Jun 22, 2014 at 08:45 AM

When you Add() to the list, the added element goes to the last index. If you want to add it in front, use Insert().

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 8r3nd4n · Jun 22, 2014 at 12:36 PM 0
Share

Good to know. I was unsure exactly how lists were populated

avatar image
0

Answer by Woj_Gabel_FertileSky · Jun 22, 2014 at 09:22 AM

msdn List methods - Add method states where items are added. In this case, at the end of it. If you reorganize your list items by time, then you need to sort that list, or use some sorted collection.

My take on it:

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class PowerUp
 {
 
     public enum EBonusType
     {
         Null,
         Slow,
         Fast,
     }
 
     public float myTime;
     public EBonusType bonusType;
     
     public PowerUp(){}
 
     public PowerUp(EBonusType powerType, float seconds)
     {
         bonusType = powerType;
         myTime = seconds;
     }
 
     public bool TickTime()
     {
         myTime -= Time.deltaTime;
         return (myTime <= 0f ) ? true : false ;
 
     }
 }


And the manager:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [System.Serializable]
 public class PowerUpManager : MonoBehaviour
 {
     public bool boolem;
     public List<PowerUp> powerUps = new List<PowerUp>();
     
     void ListTick()
     {
         for(int i = 0 ; i < powerUps.Count ; i++)
         {
             if(powerUps[i].TickTime())
             {
                 powerUps.RemoveAt(i);
             }
             if( i-1 < 0 ) continue;
             if(powerUps[i].myTime < powerUps[i-1].myTime)
             {
                 PowerUp tempPowerUp = powerUps[i-1];
                 powerUps[i-1] = powerUps[i];
                 powerUps[i] = tempPowerUp;
             }
         }
     }
     
     void Update(){
         if(boolem){
             int range = Random.Range(1,3);
             float seconds = Random.value *15f;
             powerUps.Add(new PowerUp( (PowerUp.EBonusType)range, seconds ) );
             boolem = false;
         }
         ListTick();
     }
 }



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 8r3nd4n · Jun 22, 2014 at 12:38 PM 0
Share

Cheers for that and the excellent reference to lists. I went a different way to you because of things in the rest of my code needing to work a certain way but I did get some of the ideas from you.

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

23 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

Related Questions

A node in a childnode? 1 Answer

Changes not being saved in the Inspector Editor 1 Answer

Keep adding targets to a list 2 Answers

Difference between Static, Dynamic, and Built-In Arrays 1 Answer

Adding and Removing to a Inbuild Array 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