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 TurtleWolf · Jan 27, 2011 at 09:52 AM · guiinputtoolbar

Paper, Rock, Scissor

I am attempting to recreate "Paper, Rock, Scissors" as a modest attempt to learn scripting. I appear to be adding my math function every frame. Can some one tell me what I need to change so that it adds 1 to my score every time I click on the toolbar array. I have a lot to add, but I need to make sure I am concocting my equations correctly. Thanks...

var toolbarInt = 0; var toolbarStrings : String[] = ["Paper", "Rock", "Scissors"]; var myScore = 0; var computerScore = 0; var computerChoice =1;

function OnGUI () { toolbarInt = GUI.Toolbar (Rect (25, 25, 250, 30), toolbarInt, toolbarStrings); if (toolbarInt)myScore ++; GUI.Label (Rect (25, 60, 220, 140), "The Computer has chosen " + toolbarStrings[toolbarInt]); GUI.Label (Rect (25, 120, 200, 100), "The Computer's score is " + computerScore); GUI.Label (Rect (25, 140, 200, 100), "Your score is " + myScore);

 }

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

2 Replies

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

Answer by Bunny83 · Jan 27, 2011 at 10:12 AM

The GUI.Toolbar is just a selector. Your toolbarInt will be 0, 1 or 2

OnGUI is called at least once EACH frame. You need another button to tell your game you finished your selection. Within the if block, which is executed only once when you click on the button, you can do all your stuff.

function OnGUI () {
    toolbarInt = GUI.Toolbar (Rect (25, 25, 250, 30), toolbarInt, toolbarStrings);
    if (GUI.Button(Rect(XX,YY,WW,HH),"Done"))
    {
        if (toolbarInt)myScore ++;
        // TODO: do your calculations here
    }
    GUI.Label (Rect (25, 60, 220, 140), "The Computer has chosen " + toolbarStrings[toolbarInt]);
    GUI.Label (Rect (25, 120, 200, 100), "The Computer's score is " + computerScore);
    GUI.Label (Rect (25, 140, 200, 100), "Your score is " + myScore);
}

You may also have a look at GUILayout. With it you can do all the stuff as with GUI but all Elements got positioned automatically.

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
avatar image
1

Answer by Jesse Anders · Jan 27, 2011 at 10:06 AM

I see a couple of potential problems with your code.

First, in this line:

GUI.Label (Rect (25, 60, 220, 140), "The Computer has chosen " +
    toolbarStrings[toolbarInt]);

Shouldn't that be toolbarStrings[computerChoice]? That is, don't you want to be displaying the computer's choice there, not the player's choice?

Also, I'm not sure what this line:

if (toolbarInt)myScore ++;

Is supposed to do. I don't use UnityScript so I'm not sure how 'if (toolbarInt)' is interpreted, but however it's interpreted, that's not going to give you a point every time you win a round. (If, for example, 0 is evaluated as false and all other integers as true, that'll simply add points continuously as long as the last selection was not 'paper'.)

What you probably want is something like this (untested):

var newtoolbarint = GUI.Toolbar(
    Rect(25, 25, 250, 30),
    toolbarInt,
    toolbarStrings
);
if (newtoolbarint != toolbarint) {
    toolbarint = newtoolbarint;
    // Since the computer's choice is always 'rock', only
    // paper can win. Note that if you make the computer's
    // choice variable, you can then compute whether the
    // player won or lost using the modulus operator and
    // a little arithmetic.
    if (toolbarint == 0) {
        ++myScore;
    }
}

Note that toolbarint will need to be initialized to (e.g.) -1, and reset to that value before the beginning of each round. (I think that will work correctly, but I haven't tested it.)

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

No one has followed this question yet.

Related Questions

Trying to Highlight text in order to copy and paste, but do not want the text to be editable. 1 Answer

Changing the Standalone Input Manager variables 2 Answers

Get the color under the mouse cursor 2 Answers

Why won't my object activate? 0 Answers

Unselecting a toolbar 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