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 /
  • Help Room /
avatar image
0
Question by KungFury · Oct 02, 2015 at 05:14 PM · unity 5gameobjectsetactiveinput.getbutton

Double Input.GetButton problem.

Hi guys, I'm new with Unity so don't be too harsh on me :D

I have two game objects, they are turned off at the start. With Input.GetButton("X button") i want to turn ON game object number one. When I use Input.GetButton("X button") another time I want game object number one to turn OFF, and the game object number two to turn ON. If I click again, number two gets OFF , and number one gets ON. And so on. I'm using SetActive function.

If anyone can lead me to how to go about doing this it would be greatly appreciated!

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

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

Answer by Oribow · Oct 02, 2015 at 09:25 PM

 public GameObject g1;
     public GameObject g2;
     bool isG1On = false;
 
     void Update()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             if (isG1On)
             {
                 g1.SetActive(false);
                 g2.SetActive(true);
                 isG1On = false;
             }
             else
             {
                 g1.SetActive(true);
                 g2.SetActive(false);
                 isG1On = true;
             }
         }
     }
Comment
Add comment · Show 5 · 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 KungFury · Oct 02, 2015 at 09:34 PM 0
Share

I kinda need my object two, to be ON when i push my button once, not when i hold my button pushed. if you know what I mean ;)

avatar image Oribow · Oct 02, 2015 at 09:43 PM 0
Share

Thats excatly what the code does. $$anonymous$$ay you explain your self a bit more? The state of both objects are switch when you press your button once, no need for holding.

avatar image KungFury Oribow · Oct 02, 2015 at 10:08 PM 0
Share

Ups, my bad, wrong self explanation of "GetButtonDown" function. This is working perfectly. Just one more thing. Can we use something else beside "else" statment, becouse I have read that using "else" statment is bad practice in Unity. Fix me if I am wrong.

avatar image Oribow KungFury · Oct 02, 2015 at 10:16 PM 0
Share

You are indeed wrong. "Else" is a basic keyword found in every program$$anonymous$$g language. Its say just, that if the first "if()" statement is wrong, then do the stuff in the "else{}" brackets. Do you have the source from where you read that? Would be interesting :) Besides that, I can promise you, this is the fastest solution possible. :)

Show more comments

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

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

Related Questions

gameObject.SetActive (false); Not working 0 Answers

gameObject.SetActive (false); Not working 0 Answers

Reset rotation of gameobject 1 Answer

Make a object always appear behing another object 1 Answer

How do you activate a game object ? 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