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 Shankar · May 17, 2013 at 06:40 AM · buttonarrayonguiwindowarraylist

How to display data from array,arraylist while clicking the button?

Hello All,

I have a task like displaying questions if i click the next button.

I wrote code like this:

   using UnityEngine;
   using System.Collections;

   public class Arrayquestions : MonoBehaviour {
 
 private Rect windowRect6 = new Rect(Screen.width/2,Screen.height/2, 600, 100);
 private bool questionbool1;
 public  ArrayList question,options;
 public string[,] opti;
 public GUIStyle newfontStyle;
 private bool toggleTxt1 = false,toggleTxt2 = false,toggleTxt3 = false,toggleTxt4 = false;
 string op;
 
 void Start () {
     
  question= new ArrayList(){"A pendulum length is 20m , find the Timeperiod of the pendulum ?","A pendulum timeperiod is 10sec , find the length of the pendulum ?"};
  options=new ArrayList(){"8.965s,7.965s,10.965s,9.965s,1","15m,25m,17m,18m,2"};    
   opti= new string[,]{{"8.965s","7.965s","10.965s","9.965s","1"},{"15m","25m","17m","18m","2"}};
 
 }
 
 // Update is called once per frame
 void OnGUI () {
     
      if(GUI.Button(new Rect(Screen.width-(Screen.width/4),523,130,60),"Assignment"))
     {
         questionbool1 = true;
     }
     if(questionbool1)
     {
      windowRect6 = GUI.Window(4, windowRect6, DoMyWindow6, "");    
     }
     
 }
 void DoMyWindow6(int windowID)
 {
      if(GUI.Button(new Rect(450,50,70,30), "NEXT"))
     {
         print ("hi click");
 //foreach(string s in question)
     for (int i=0;i<2;i++)
     {
          GUI.Label(new Rect(10,10,500,100),question[i].ToString(),newfontStyle);
     }
 //foreach(string o in options)
         //for(int j=0;j<4;j++)
     //{
         
     for(int i=0,j=0; i<2 && j<4; i++,j++)
         {
    toggleTxt1 = GUI.Toggle(new Rect(30, 50, 100, 40), toggleTxt1, "");
    toggleTxt2 = GUI.Toggle(new Rect(130, 50, 100, 40), toggleTxt2, "");
    toggleTxt3 = GUI.Toggle(new Rect(250, 50, 100, 40), toggleTxt3, "");
    toggleTxt4 = GUI.Toggle(new Rect(350, 50, 100, 40), toggleTxt4, "");
             
             
         }
     //}        
     }
 }
     }


If i click the NEXT button I'm not able to get the questions.

Comment
Add comment · Show 7
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 jayhatmaker · May 17, 2013 at 08:33 AM 0
Share

Hi, I'm not an expert on this label thing, but I'll try to answer.

It looks like you are going through all your question array in a for loop?

You should have some controlling variable like...

int currentQuestion;

Then, when your next button is pressed, you add.

currentQuestion++;

Then in your display question function...

GUI.Label(new Rect(10,10,500,100), question[currentQuestion] ,newfontStyle);

something like this should do the trick.

Also, a bit off, but I wouldn't use numbers like "i<2" in the code. You might want to count your question array, and use that number. That way, if you add more questions, you don't have to worry about these magic numbers being corrected.

WThis previous question may help you too.W

avatar image Fattie · May 17, 2013 at 01:12 PM 0
Share

FWIW generally use List .. forget about arrays

avatar image Eric5h5 · May 17, 2013 at 01:27 PM 1
Share

@Fattie: you should use arrays if the length of the array doesn't need to change. You should not, however, use ArrayList.

avatar image Fattie · May 17, 2013 at 01:29 PM 0
Share

I'm down on arrays since you told me about List man! ;-)

for me it's List or spaghetti computers

avatar image Eric5h5 · May 17, 2013 at 02:49 PM 0
Share

Arrays can have significantly better performance, particularly for primitive types (as in, 5X faster in that case, not just a little bit). So really you should use arrays where feasible, namely, if the size doesn't change. There's no reason to be "down on" them. Lists are backed by arrays anyway, they're just a "nice" way to do System.Array.Resize and so on. Because that's what's really happening with Lists.

(For advanced users needing performance optimizations: always use arrays, period, even if the size does change. As I mentioned, you can use System.Array.Resize, and you can use System.Array.Copy to insert and remove items. This is what Lists do anyway, just with nicer syntax. Just make sure the extra coding is actually worth doing first.)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Shankar · May 18, 2013 at 08:10 AM

using UnityEngine; using System.Collections;

public class Arrayq : MonoBehaviour {

     // Use this for initialization
     private Rect windowRect6 = new Rect(Screen.width/2,Screen.height/2, 600, 100);
     private bool questionbool1,questi;
     public  string[] question,options1,options2,options3,options4;
     public  ArrayList useranswers,correctanswer;
     public string[,] opti;
     public GUIStyle newfontStyle;
     private bool toggleTxt1 = false,toggleTxt2 = false,toggleTxt3 = false,toggleTxt4 = false;
     public int qno=0;
     string op;
     private int count=0;
     void Start () {
      questi=true;
      question= new string[]{"A pendulum length is 20m , find the Timeperiod of the pendulum ?","A pendulum timeperiod is 10sec , find the length of the pendulum ?"};
      options1=new string[]{"8.965s","15m"};
      options2=new string[]{"7.965s","25m"};
      options3=new string[]{"10.965s","17m"};
      options4=new string[]{"9.965s","18m"};
      correctanswer = new ArrayList();
      useranswers = new ArrayList();
      correctanswer.Add("1");
     correctanswer.Add("2");
     }

     // Update is called once per frame
     void OnGUI () {

     if(GUI.Button(new Rect(Screen.width-(Screen.width/4),523,130,60),"Assignment"))
             {
                     questionbool1 = true;
             }
             if(questionbool1)
             {
                    windowRect6 = GUI.Window(4, windowRect6, DoMyWindow6, "");
             }

     }
     void DoMyWindow6(int windowID)
     {
     
     GUI.Label(new Rect(10,10,500,100),question[qno].ToString(),newfontStyle);
    if(GUI.Toggle(new Rect(30, 50, 100, 40), toggleTxt1, options1[qno].ToString()))toggleTxt1 = setMeOnly();
    if(GUI.Toggle(new Rect(130, 50, 100, 40), toggleTxt2, options2[qno].ToString()))toggleTxt2 = setMeOnly();
    if(GUI.Toggle(new Rect(250, 50, 100, 40), toggleTxt3, options3[qno].ToString()))toggleTxt3 = setMeOnly();
    if(GUI.Toggle(new Rect(350, 50, 100, 40), toggleTxt4, options4[qno].ToString()))toggleTxt4 = setMeOnly();   
     
     if(GUI.Button(new Rect(450,50,70,30), "NEXT"))
     {
         toggleTxt1=toggleTxt2=toggleTxt3=toggleTxt4= false;
         if(toggleTxt1)
         {
             useranswers.Add("1");
         }
         else if(toggleTxt2)
         {
             useranswers.Add("2");
         }
         else if(toggleTxt3)
         {
             useranswers.Add("3");
         }
         else if(toggleTxt4)
         {
             useranswers.Add("4");
         }
         
         if(qno<1)
         {
             qno++;
     GUI.Label(new Rect(10,10,500,100),question[qno].ToString(),newfontStyle);
       if(GUI.Toggle(new Rect(30, 50, 100, 40), toggleTxt1, options1[qno].ToString()))
         toggleTxt1 = setMeOnly();    
     if(GUI.Toggle(new Rect(130, 50, 100, 40), toggleTxt2, options2[qno].ToString()))
         toggleTxt2 = setMeOnly();
     if(GUI.Toggle(new Rect(250, 50, 100, 40), toggleTxt3, options3[qno].ToString()))
         toggleTxt3 = setMeOnly();
      if(GUI.Toggle(new Rect(350, 50, 100, 40), toggleTxt4, options4[qno].ToString()))
         toggleTxt4 = setMeOnly();   

// print(useranswers[1]);

         }
     }
     }
     bool setMeOnly(){
     toggleTxt1=toggleTxt2=toggleTxt3=toggleTxt4= false;
     return true;    
     }
 

}

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

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

16 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

Related Questions

A few GUI related questions. 1 Answer

OnGUI Button doesn't execute until I release the button 1 Answer

AudioSource Array Index is out of range 3 Answers

How could I show buttons if I am using array list? 0 Answers

OnGUI() works on Editor but not on Android Device 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