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 kaitoren · Jul 16, 2018 at 01:03 PM · objecttextlistbuttons

Several objects, same script, different values with no duplications

I'm making a little quiz game with multiple choices for practicing. Each answer button has a random item taken from a string list. I mean, the list contains elements and with this script I "paste" a random string element from that list in the UI text box to show an answer:

 public Text answerText;
 
     [SerializeField]
     private int randomAnswerIndex;
 
     void Start () {
         randomAnswerIndex = Random.Range(0, languageAnswers.Count);
         answerText.text = languageAnswers[randomAnswerIndex];
 
     }

Each answer button has this script referenced, but I'm lost in how to avoid that an item text from the list shows twice, i.e., the word "English" in two different buttons at the same time. How can I solve it? If it's with an "if" statement, how can I access to the different button texts from the script? I'm trying to use "FindGameObjectWithTag", but it doesn't work.

Thanks!

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 akaBase · Jul 16, 2018 at 04:06 PM

Remove it from the list.

public Text answerText;

  [SerializeField]
  private int randomAnswerIndex;
 
  void Start () {
      randomAnswerIndex = Random.Range(0, languageAnswers.Count);
      answerText.text = languageAnswers[randomAnswerIndex];
      languageAnswers.RemoveAt(randomAnswerIndex);
  }

}

But I think a better way to do this would be to have a main script that you can loop through the buttons rather than have them all triggered at Start().

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 kaitoren · Jul 16, 2018 at 08:13 PM 0
Share

Thanks for the answer. The option of removing the used elements from the list doesn't work because the list is inside the script attached to every button, so each button has its personal list that acts independently from the others. I'll work in the second option. When you say 'loop' do you mean that I need to activate the buttons one after another with a coroutine? Any other help to deal with this problem will be really appreciated.

avatar image akaBase kaitoren · Jul 16, 2018 at 09:22 PM 0
Share

I mean something like this

 public Button button1;
 public Button button2;
 public Button button3;
 public Button button4;

 void Start()
 {
     List<Button> buttons = new List<Button> { button1, button2, button3, button4 };
     List<string> answers = SomeAnswersClass.getanswers();
     for (int count = 0; count < buttons.Count - 1; count++)
     {
         int randIndex = $$anonymous$$athf.FloorToInt(Random.Range(0, (answers.Count - 1) + 0.5f));
         buttons[count].GetComponentInChildren<Text>().text = answers[randIndex];
         answers.RemoveAt(randIndex);
     }
 }

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

157 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Question regarding Buttons 0 Answers

Null Reference Exception 0 Answers

NullReferenceException : Object reference not set to an instance of an object? 0 Answers

Destroy Object and Text 0 Answers

instancing several buttons with different text 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