• 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
Question by asfasdfasdf · Jan 11, 2013 at 09:55 PM · noobpaperrock

What is wrong with my RockPaperSissors script?

I am a complete noob at programming and eger to lern, but i cant see to find out what the problem with this script it, after i chose my options nothing happens, it used to work, but then i changed "computer" from a random number to another player

 var ccc : GameObject;
 
 function OnGUI()
 {
     ccc = GameObject.Find("Camera");
 
     // check to see if the human has pressed a button
     var human = -1;
 
     // draw and check each button
     if (GUI.Button(Rect(40, 10, 80, 60), "Attack"))
         human = 0;
     
     if (GUI.Button(Rect(140, 10, 80, 60), "Range"))
         human = 1;
     
     if (GUI.Button(Rect(240, 10, 80, 60), "Defence"))
         human = 2;
     
     // if the human has pressed something, do the game logic.
     if(human > -1)
     {
         // choose a value for the computer
         var computer = -1;
         if (GUI.Button(Rect(40, 10, 80, 60), "Attack"))
             computer = 0;
         if (GUI.Button(Rect(140, 10, 80, 60), "Range"))
             computer=1;
         if (GUI.Button(Rect(240, 10, 80, 60), "Defence"))
             computer=2;
         
         if(computer > -1)
         {
             // set the results to equal to start
             var results=0;
             ccc.animation.Play("Intro");
         
             // did we win?
             if(human == 0 && computer == 2) results = 1;
             if(human == 1 && computer == 0) results = 1;
             if(human == 2 && computer == 1) results = 1;
             
             // did we lose?
             if(human == 0 && computer == 1) results = -1;
             if(human == 1 && computer == 2) results = -1;
             if(human == 2 && computer == 0) results = -1;
         }
     }
 }
Comment

People who like this

0 Show 1
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 AndyMartin458 · Jan 11, 2013 at 10:17 PM 1
Share

I suggest that you create the var computer and var human variables above the OnGUI function as class variables. That would be a good start.

Where is it that you've change computer from another number to player? In this script it looks like it is just a number.

How do you know that it's not working? I don't see anywhere where you're displaying the numbers. Perhaps give a little more explanation for what you're doing, and also your OnGUI function has so many brackets yet isn't really following typical indentation. That's just a suggestion because it's making it harder for me to read.

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Wolfram · Jan 12, 2013 at 12:28 AM

I haven't really used UnityGUI, but keep in mind that the GUI is build "on the fly" while executing OnGUI() each frame. So there will be three buttons. If you click one of them, "human" will be set, and only in this case three new buttons for the computer will be rendered. However, a) the button click has already been evaluated, b) these buttons overlay the "human" buttons at identical position. So none of the thre computer-"if"-conditions can ever be true.

If that is not the solution, you'd have to explain the problem some more. What are these computer buttons supposed to do, how do you intend to choose a value for computer, what do you mean by "i changed "computer" from a random number to another player", what is the problem you are seeing?

Comment

People who like this

0 Show 1 · 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 AndyMartin458 · Jan 12, 2013 at 04:43 AM 0
Share

Yes, that's what is strange. I think that by "changed computer to player", this really means that buttons have been added for the computer. That is definitely why it is important to set the values of a class scope variable.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Multiple Cars not working 1 Answer

Script outdated i think, Unity 4 - Compiler errors galore 1 Answer

Enter Trigger, display Text, then delete object 1 Answer

Only access door if it's not locked? 1 Answer

Noob Question! Unexpected token: if. 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