- Home /
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;
}
}
}
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.
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?
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
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Need help with begginner script! 1 Answer
Where do I assign GUI scripts? 1 Answer
Forward movement Slow? Delta.time? Frame Rate? 0 Answers
Script outdated i think, Unity 4 - Compiler errors galore 1 Answer