- Home /
Duplicate Question
Trying to implement an if or statement
Hello everyone,
I think my issue here is simply one of placement, and would be appreciative and humbled if someone might be able to provide an assist. I'm trying to implement an or if statement, and can't seem to find a lot of documentation on them. (found one, which I tried using, but am running into an error) Here's my statement:
if(GUI.Button(Rect(400,775,100,50),"Yes"))||if(Input.GetButtonDown("Action")){
(if you need more code, no problem, but I figured that the issue is probably all contained in that line)
All I'm trying to do is allow either a click of the GUI button OR the gamepad "action" button to do the action. Currently I'm getting an "unexpected token" error at the ||. Thanks for any assistance. God bless.
Answer by clunk47 · Oct 09, 2013 at 04:35 PM
You only need to use 'if' once in a statement like this. Also, keep the entire statement in a single encapsulation. if(this || that){//DoSomething}; This particular case isn't really best to use in GUI code.
if(GUI.Button(Rect(400,775,100,50),"Yes") || Input.GetButtonDown("Action"))
{
//DoSomething;
}
This may be because GUI code is fired twice per frame. You will probably need to use your GetButtonDown inside of Update(), and only GUI code in OnGUI function. Try creating a new method that will do what you need.
using UnityEngine;
using System.Collections;
public class Example : $$anonymous$$onoBehaviour
{
int test = 0;
void Update()
{
if(Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.E))
{
Send();
}
}
void OnGUI()
{
if(GUI.Button (new Rect(0, 0, 256, 256), "Yes"))
{
Send();
}
}
void Send()
{
test++;
print (test);
}
}
cool Clunk, thanks. Looks like you're using C#, which shouldn't be too mean a feat to convert over. To make sure I'm understanding, essentially what you're saying is: $$anonymous$$eep the On Gui and Getbuttondown in separate areas. Each would individually call the (same) function which would destroy the game object and add it to my inventory, essentially eli$$anonymous$$ating the need for the or statement.
Correct. Here this code is in JS.
#pragma strict
var test : int = 0;
function Update()
{
if(Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.E))
{
Send();
}
}
function OnGUI()
{
if(GUI.Button (Rect(0, 0, 256, 256), "Yes"))
{
Send();
}
}
function Send()
{
test++;
print (test);
}
clunk, you are seriously awesome, thank you!!!!!! While I've encountered another $$anonymous$$or issue (the chatbox doesn't disappear now) my main problem has been solved, and I'm sure the new one will just take a bit of time before being resolved. Seriously, you rock.
EDIT: New issue isn't as bad as I thought. If I let the GUI text complete, the problem doesn't occur. If I interrupt it, that's when the GUI box won't disappear.
Thanks for the kind words. I hope to be able to help with any near future issues at hand.
Follow this Question
Related Questions
how can i have an if statement to kill a player if they dont have an item 2 Answers
Strange errors in javascript 2 Answers
"or": HELP PLEASE!!! 1 Answer
Using the same controller button twice 2 Answers
Object will not instantiate 1 Answer