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 /
  • Help Room /
avatar image
0
Question by Stoozey · Dec 22, 2017 at 06:27 AM · arraybuttonsgameobjects

How would I select GameObjects from an array one by one?

So basically, I have a song selection menu in my game, I am using two buttons as arrows, one on the left and one on the right, obviously, the left button would go - in the array and the right arrow would go + in the array, but nooby me, can't achieve this, I can tell its a super simple process, but what I have just goes from 0 to 14 instead of going from 0,1,2,3... etc. (array of 15 items). The "Song Ref" is the selection of the song which gives the output.

 public Button leftBtn;
 public Button rightBtn;

 [Space]

 [Header("Array Ref")]
 public GameObject songRef;

 [Space]

 [Header("Song Game Objects")]
 public GameObject[] array;
 public int selection;

 private bool leftButtonIsPressed;
 private bool rightButtonIsPressed;


 void Start()
 {
     selection = 0;
 }

 void RightButtonPressed()
 {
     if (selection < array.Length - 1) //because size starts at 1, arrays at 0
     {
         selection++;
     }
 }

 void LeftButtonPressed()
 {
     if (selection > 0)
     {
         selection--;
     }
 }

 void Update ()
 {

     rightBtn.onClick.AddListener(RightButtonPressed);

     leftBtn.onClick.AddListener(LeftButtonPressed);

     songRef = array[selection];
     //change song using refrence

 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by DreadKyller · Dec 22, 2017 at 06:41 AM

From what I can see of your problem, it seems you're adding multiple of the same listener to the button press events. Every frame you're adding another listener, so by frame 13+ pressing a button calls the function 13+ times, thus giving you your issue. Set the event handlers in the Start() method instead of the Update() method, as Start only gets ran once it should only register it once.

Comment
Add comment · Show 2 · 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 Stoozey · Dec 22, 2017 at 06:44 AM 0
Share

Oh my god thank you so much, I'm so stupid for not seeing this.

avatar image DreadKyller Stoozey · Dec 22, 2017 at 06:48 AM 0
Share

I'm guessing that means it worked? I've never tried to add the same handler twice so for all I knew Unity ensured no duplicates, this was just the only thing I could think of. And don't call yourself stupid :)

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

130 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 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

Trying to get gameobject from one list to follow gameobjects in another list 1 Answer

Cycle through objects on mouse click 1 Answer

Cannot click 4.6 GUI when attached to a Game object 1 Answer

Unity won't print last item in my array for my GameObjects 0 Answers

Can I create a GameObject Array then fill UI text elements and then somehow use a For Loop to drop an Array of Int values inside those UI text boxes from largest to lowest? 0 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