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 Graveyard Gamers · Nov 20, 2015 at 01:46 AM · canvasarraysbuttons

Adding a new string to my array after each button click.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 
 //This class will be storing words that the 
 //user enters in to be used.
 
 public class CustomWords : MonoBehaviour {
 
     public Button addword;
     public Canvas can;
     public InputField wordtext;
     public string[] words;
     public void Start() 
     {
         addword = addword.GetComponent<Button> ();
         can = can.GetComponent<Canvas> ();
         wordtext = wordtext.GetComponent<InputField> ();
         words = new string[10];
 
     }
 
     public void CustomWord()
     {
         //with each click of the button it adds the word in the inputfield to the array list.
         words[0] = wordtext.text;
 
 
     }
 
 
 }


So far so good on this little bit of code that I wrote. I still new to arrays and trying to figure out how to add the word I put in the inputfield to my string array. For example I write the word "The" and click the addword button, it adds that word to the array element 0, now the next time I do it I want it added to the array element 1 and so on till I get to 10. I think I need to use a for loop, but not 100% sure.

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 ZefanS · Nov 20, 2015 at 09:15 AM

In order to fill the array from index 0 to 9, simply use a counter that gets incremented on each click / addition to the array.

  using UnityEngine;
  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine.UI;
  
  //This class will be storing words that the 
  //user enters in to be used.
  
  public class CustomWords : MonoBehaviour
  {
      public Button addword;
      public Canvas can;
      public InputField wordtext;
      public string[] words;
      public int currentIndex; //This keeps track of the index
 
      public void Start() 
      {
          addword = addword.GetComponent<Button> ();
          can = can.GetComponent<Canvas> ();
          wordtext = wordtext.GetComponent<InputField> ();
          words = new string[10];
          currentIndex = 0; //Make sure we start at 0
      }
  
      public void CustomWord()
      {
          //With each click of the button the word in the inputfield is added to the array
          words[currentIndex] = wordtext.text; //Add the word at the current index
 
          if (currentIndex < 9)
          {
              currentIndex++; //Increase the index to the next position in the array
          }
          else
          { 
             //Change this code to handle the full array
             currentIndex = 0; //Reset back to 0 and start filling the array from the start
          }
      }
  }

Currently this just lets the user loop endlessly and keep overwriting the array. You may want to handle things differently when the array is full. To do that, simply change the code in the else statement.

Hope this helps.

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 Graveyard Gamers · Nov 20, 2015 at 10:01 PM 0
Share

This is working perfect for what I am doing. :)

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

36 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

Related Questions

How do I set my canvas / camera to represent what will be put on phone screen? 0 Answers

Two buttons are staying in front of every other ui element regardless of where I put them in the hierarchy 1 Answer

Android UI push Multiple buttons Help 0 Answers

Check state of variable for each item in an Array 1 Answer

Canvas not loading on first click of button 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