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 /
This question was closed Oct 09, 2013 at 04:43 PM by Fattie for the following reason:

Duplicate Question

avatar image
1
Question by Conect11 · Oct 09, 2013 at 04:28 PM · if-statements

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.

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

  • Sort: 
avatar image
4
Best Answer

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;
  }


Comment
Add comment · Show 6 · 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 clunk47 · Oct 09, 2013 at 04:53 PM 1
Share

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);
     }
 }
avatar image Conect11 · Oct 09, 2013 at 04:58 PM 1
Share

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.

avatar image clunk47 · Oct 09, 2013 at 05:04 PM 2
Share

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);
 }
 
avatar image Conect11 · Oct 09, 2013 at 05:26 PM 2
Share

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.

avatar image clunk47 · Oct 09, 2013 at 05:33 PM 1
Share

Thanks for the kind words. I hope to be able to help with any near future issues at hand.

Show more comments

Follow this Question

Answers Answers and Comments

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

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


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