- Home /
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.
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.
@Fattie: you should use arrays if the length of the array doesn't need to change. You should not, however, use ArrayList.
I'm down on arrays since you told me about List man! ;-)
for me it's List or spaghetti computers
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.)
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;
}
}