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 ltrout1999 · Dec 22, 2016 at 05:07 AM · uirandomstring

Randomly Changing Strings?

I'm creating a trivia style game and I setup a panel with a Question Text and then another one with 4 buttons, one for each answer choice. I then setup an array that consists of a question and an array of answers with a bool for isCorrect inside of that array. Now, I can't get the buttons to change their strings to one of the answer strings in the array? Here is my code so far:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class GameManager : MonoBehaviour
 {
     public Question[] questions;
 
     public Text questionTextUI;
     public Text[] ansTextUI;
 
     Question question;
 
     void Start ()
     {
         ChooseQuestion();  
     }
 
     void ChooseQuestion ()
     {
         //Choosing Question Text
         questionTextUI.text = questions[0].questionsText[0];
 
         //Choosing Answers Text
         for (int i = 0; i < ansTextUI.Length; i ++)
         {
             //ansTextUI[i].text = questions[0].answers[0].answersText;
             for (int o = 0; o < questions[0].answers.Length; o ++)
             {
                 ansTextUI[i].text = questions[0].answers[o].answersText;
             }
         }
     }
 }


Question.cs:

   [System.Serializable]
     
     public class Question
     {
          public string[] questionsText;
          public Answer[] answers;
     }

Answer.cs

   [System.Serializable]
     
     public class Answer
     {
          public string answersText;
          public bool isCorrect;
     }


Comment
Add comment · Show 3
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 ltrout1999 · Dec 22, 2016 at 05:17 AM 0
Share

@dhore , can you help?

avatar image dhore · Dec 22, 2016 at 07:05 AM 0
Share

Could you show us the code for your Question class?

avatar image ltrout1999 dhore · Dec 22, 2016 at 06:22 PM 1
Share

I updated the question with the classes.

1 Reply

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

Answer by dhore · Dec 22, 2016 at 07:44 PM

Your Answer array variable in your Question class is called "answer" whereas on line 31 of your GameManager class you reference it as "answers" (with an s) ..typo?

Also, I don't think you need 2 for loops.. just do this:

 for (int i = 0; i < ansTextUI.Length; i ++)
 {
     ansTextUI[i].text = questions[0].answer[i].answersText;
 }

As long as ansTextUI.Length == questions[0].answers.Length

Comment
Add comment · Show 5 · 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 ltrout1999 · Dec 22, 2016 at 07:48 PM 0
Share

I forgot to mention I didn't have my other computer with me at the moment so I rewrote the Question and Answer class. In the actual scene there aren't any errors, it just changes all of the answer's text to the 4th index (3 since it starts at 0) in the array ins$$anonymous$$d of putting one array index with one answer, another with another, and so on until all 4 strings are on all 4 answer texts. And the length's are the same. 4 answers, and 4 strings.

avatar image ltrout1999 · Dec 23, 2016 at 03:34 AM 0
Share

Also the reason I have it looping twice is because one is for it to loop through each of the 4 buttons and the second is to loop through each of the 4 strings in the array for answers.

avatar image dhore ltrout1999 · Dec 23, 2016 at 10:02 AM 0
Share

I didn't really explain it very well, but the code example that I gave you above is actually a fix for your for loop. You only need the 1 loop as you want to assign the values with matching indexes like so:

ansTextUI[0].text = answers[0].answersText;

ansTextUI[1].text = answers[1].answersText;

ansTextUI[2].text = answers[2].answersText;

ansTextUI[3].text = answers[3].answersText;

So just replace lines 26-33 with the code I posted above and you should be good ;)

avatar image ltrout1999 dhore · Dec 25, 2016 at 04:47 AM 1
Share

Oh wow, that does make sense now! I figured I'd have to have a loop for each one, but that simplifies it. Thanks a lot! :) $$anonymous$$erry christmas

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.

Follow this Question

Answers Answers and Comments

117 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

Related Questions

BCE0022: Cannot convert 'String' to 'int' 0 Answers

Why is my listdisplaying that an item was removed? 0 Answers

is there simple way to put random sprite to gameobject multi child with no repating. 1 Answer

Pick a random string from a string array C# 1 Answer

Memory problem on android/ios 1GB ram with string[]? 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