Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
-1
Question by jdog_ · Jan 25, 2017 at 10:30 PM · unity5if-statementsifif statementif-statement

Can I Put a IF In a IF?!?

Can I Put a IF in A IF? If not How do I do It??

EXAMPLE: If Cursor Is Touching Lever AND ALSO Pressing Action Button(E) Than Play Animation.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LeverON : MonoBehaviour {
     public GameObject Cursor;
     public GameObject Lever;
     
 
     void OntriggerEnter (Collider other) {
         
         if (other.CompareTag ("Cursor")) {
             
             Debug.Log("Cursor Has Collided")
             
 
     
             if(Input.GetButtonDown("Action")) {
 
             Debug.Log("Play Animation");
 
             Lever.GetComponent<Animation> ().Play ("LeverOn");
 
 
 
             }
 
 
         }
 }
 }
Comment
Add comment · Show 4
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 Cherno · Jan 25, 2017 at 10:56 PM 1
Share

Sure you can. Just make sure you get the curly brackets right. Do you get any erros with the code you posted? If yes, please say so.

avatar image jdog_ Cherno · Jan 26, 2017 at 03:13 AM 0
Share

Yeah, I use the below code and it works. I think I placed the IF's wrong.

avatar image Habitablaba · Jan 25, 2017 at 11:42 PM 1
Share

Short answer: yes.

Longer answer: sure, but I'm not sure that's actually the solution to what you're trying to do here. It looks like you want to detect when the user has pressed the action button while the cursor is in some trigger zone.
For this, you really should have a private bool variable at the class level that you set to true in your OnTriggerEnter, and false in an OnTriggerExit. Then, in update, check for your Action button press, and make sure the bool is true at that time.
What you have will only work if the user happens to press the action key on the same frame that the cursor enters the trigger.

avatar image Pyrotechnic · Feb 07, 2017 at 02:18 AM 0
Share

you are missing a semicolon.

2 Replies

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

Answer by juicyz · Jan 26, 2017 at 12:12 AM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
      
 public class LeverON : MonoBehaviour {
    public GameObject Cursor;
    public GameObject Lever;

    // As long as a gameobject stays within the trigger, this will be called over and over
    void OnTriggerStay(Collider other) {
       if(Input.GetButtonDown("Action")) {
          if (other.CompareTag ("Cursor")) {
             Debug.Log("Play Animation");
             Lever.GetComponent<Animation> ().Play ("LeverOn");
          }
       }
    }
 }
Comment
Add comment · Show 3 · 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 jdog_ · Jan 26, 2017 at 03:16 AM 0
Share

Thank You So very $$anonymous$$uch!! I was about to go CRAZY! God Bless You:D

avatar image losingisfun · Jan 26, 2017 at 03:21 AM 1
Share

Yes you can do this, but just for future reference, it works the same to use && within the same IF statement: if (Input.GetButtonDown("Action") && other.CompareTag ("Cursor")) { //do stuff }

avatar image jdog_ losingisfun · Jan 26, 2017 at 03:48 AM 0
Share

Thank you! your awesome!

avatar image
2

Answer by Sppy653 · Jan 26, 2017 at 08:36 AM

Yeah that's perfectly fine. Its just embedded if statements, more than common. I would suggest maybe using

 if((other.CompareTag("Cursor")) && (Iput.GetButtonDown("Action"))
 {
 
 }

though as its just minor inefficiencies but if that seems to confusing don't worry about 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

11 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

Related Questions

Make script affect only one GameObject instead of making all object with this script be affected. 0 Answers

If statement not working 1 Answer

If statement question 3 Answers

How to make several conditions for an if statement? 5 Answers

object passes certain point in space 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