- Home /
How to display data in an array
Hi,
In my games, i have a hint button. Mean that players can have only 3 hints. What i want to ask is how can i tell the player how much hints left for them to use?
GUI.Button(Rect((Screen.width/2)+320, (Screen.height/2)+280, 100, 50),"Hint3");
GUI.Button(Rect((Screen.width/2)+320, (Screen.height/2)+280, 100, 50),"Hint2");
GUI.Button(Rect((Screen.width/2)+320, (Screen.height/2)+280, 100, 50),"Hint1");
private var hintList : Array = new Array();
so when the player click hint, i will need to push one of the hints from the list to the player. HOw can i go about doing it??
Answer by AlucardJay · Mar 24, 2012 at 04:50 PM
just an example :
private var hintList : Array = new Array();
private var hintsRemaining : int = 3;
private var hintGiven ;
private var showingHint : boolean = false;
function Start () {
hintList[3] = "wash your feet";
hintList[2] = "buy some milk";
hintList[1] = "feed the cat";
hintList[0] = "get some sleep";
}
function Update () {
//
}
function showHint () {
showingHint = true;
yield WaitForSeconds (1);
showingHint = false;
}
// OnGUI
function OnGUI () {
if ((hintsRemaining > 0)&&(!showingHint)) {
if (GUI.Button(Rect(10, 10, 100, 50), "Hint " + hintsRemaining)) {
hintGiven = hintList[hintsRemaining];
print ("hintsRemaining "+hintsRemaining+" : " + hintList[hintsRemaining]+" : hintGiven " + hintGiven);
showHint();
hintsRemaining -= 1;
}
}
if (showingHint) {GUI.TextArea(Rect(120, 10, 150, 35), "" + hintGiven);}
// reset button
if ((hintsRemaining <= 0)&&(!showingHint)) {
if (GUI.Button(Rect(10, 75, 100, 50), "Start Again")) {
hintsRemaining = 3;
}
}
}
Thank you for the answer,i got the error for the print statement. How come?? print ("Hint "+(4-hintsRemaining)+" : " + hintList[hintsRemaining]); ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count. Parameter name: index 3
And to add on, after the 3hints are used up, i want to make the button disappear. So how can i go about doing it?
this is just an example, answering your question 'how can i tell the player how much hints left for them to use? '
I added a var hintsRemaining , and if you notice , it says what hint number on the button. Question answered. The text area was just to show the value loaded depending on hintsRemaining when the button is pressed. (Qn: so when the player click hint, i will need to push one of the hints from the list to the player.)
And after 3 hints, the button DOES disappear.
If you want a yield loop and hint display , that's more code, but not in the question.
EDIT : I have just updated the script in the answer. Try that , and let me know the result.
Hi alucardj, thank you for the reply. I have tried and i got the same error just on different lines and i don't know what wrong. hintGiven = hintList[hintsRemaining]; ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count. Parameter name: index 3
I did not do anything do the update function. Do i have to do anything in the update function?? Like decrement the hintRemaining?
hintRemaining is decremented after the hint is shown.
click button => hintGiven gets updated => showHint => wait => don't showHint => hintsRemaining -= 1;
Here is the Same code again, just incase there is a problem with the answer being updated on your end (and I also added a reset button) :
private var hintList : Array = new Array();
private var hintsRemaining : int = 3;
private var hintGiven ;
private var showingHint : boolean = false;
function Start () {
hintList[3] = "wash your feet";
hintList[2] = "buy some milk";
hintList[1] = "feed the cat";
hintList[0] = "get some sleep";
}
function Update () {
//
}
function showHint () {
showingHint = true;
yield WaitForSeconds (1);
showingHint = false;
}
// OnGUI
function OnGUI () {
if ((hintsRemaining > 0)&&(!showingHint)) {
if (GUI.Button(Rect(10, 10, 100, 50), "Hint " + hintsRemaining)) {
hintGiven = hintList[hintsRemaining];
print ("hintsRemaining "+hintsRemaining+" : " + hintList[hintsRemaining]+" : hintGiven " + hintGiven);
showHint();
hintsRemaining -= 1;
}
}
if (showingHint) {GUI.TextArea(Rect(120, 10, 150, 35), "" + hintGiven);}
// reset button
if ((hintsRemaining <= 0)&&(!showingHint)) {
if (GUI.Button(Rect(10, 75, 100, 50), "Start Again")) {
hintsRemaining = 3;
}
}
}
yes. i followed what you have shown. I still got this error. ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count. Parameter name: index 3 Why will i get this ideas?? Anyway the way you do is what i want.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Very simple script - why isn't it working? 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Creating & accessing a function from a diffrent "OnTriggerEnter" Function 1 Answer
Setting Scroll View Width GUILayout 1 Answer