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 missypooh · Mar 24, 2012 at 04:18 PM · scriptingbasics

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??

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
Best Answer

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;
         }
     }
 }
Comment
Add comment · Show 7 · 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 missypooh · Mar 24, 2012 at 05:52 PM 0
Share

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?

avatar image AlucardJay · Mar 24, 2012 at 06:00 PM 0
Share

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.

avatar image missypooh · Mar 25, 2012 at 02:06 AM 0
Share

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?

avatar image AlucardJay · Mar 25, 2012 at 02:46 AM 0
Share

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;
         }
     }
 }
avatar image missypooh · Mar 25, 2012 at 05:04 AM 0
Share

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.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

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


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