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 /
avatar image
0
Question by swifteria · Nov 27, 2014 at 05:15 PM · c#gameobjectenable

How Do I Enable a GameObject upon Button Press?

I want a green spotlight thing to enable when I press 'tab'. Here is the code I have so far. This script is on the green spotlight itself.

 using UnityEngine;
 using System.Collections;
 
 public class SelectPlayer : MonoBehaviour 
 {
 
     // Use this for initialization
     void Start () 
     {
         if(Input.GetKey(KeyCode.Tab))
         {
             gameObject.SetActive(true);
         }
 
     }
     
     // Update is called once per frame
     void Update () 
     {
     
     }
 }
 

Nothing happens when I press tab. I've tried setting the GameObject on and off manually in-client before testing, and either way it doesn't work. Thanks in advance!

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

2 Replies

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

Answer by YoungDeveloper · Nov 27, 2014 at 05:24 PM

You must process input in Update, start is called only once.

 void Update () {
     if(Input.GetKeyDown(KeyCode.Tab)){
         gameObject.SetActive(!gameObject.activeSelf);
         Debug.Log("set");
     }
     Debug.Log ("Update is called");
 }

This will set gameobject active state to opposite. Notice, that you will be able only to disable the gameobject, as disabled components aren't updated, "Update is called" wont be called. So in this case there's really no way of re-enabling the gameobject from same gameobject.

The solution is to process SetActive() from other gameobject with active state is always true. It can either be parent or completely separate gameobject.

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 swifteria · Nov 27, 2014 at 05:44 PM 0
Share

Okay. I'm new to coding, so what you said in about the SetActive makes little sense to me :(. I know its asking for a lot, but could we do something where the object is turned off, i press tab, and then the object appears and fades out after a couple of seconds? Or would it be simpler just to do what you said? If so, please explain like I'm five. Thanks so much :)

avatar image swifteria · Nov 27, 2014 at 05:50 PM 0
Share

OR!: Could we just have the object fade in our out when tab is pressed?

avatar image YoungDeveloper · Nov 27, 2014 at 05:53 PM 0
Share

In most cases, disabling whole gameobject isn't needed. You could just toggle simple bool and then check the state. Try and run this.

 bool flag = true;
 
 void Update () {
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Tab)){
         flag = !flag; //set flag to opposite value
     }
 
 
     if(flag){       
         Debug.Log ("yes");
     }
 
     else{
         Debug.Log ("no");
     }
 }


avatar image Kiwasi · Nov 27, 2014 at 09:12 PM 0
Share

Generally disabling a whole GameObject is more efficient the bool checking.

avatar image YoungDeveloper · Nov 27, 2014 at 10:03 PM 0
Share

Yes, but he needs extra functionally even if one module is disabled.

avatar image
1

Answer by Kiwasi · Nov 27, 2014 at 05:19 PM

You can't have a GameObject set itself to active. This is because Unity does not do any call backs on inactive GameObjects.

Put this script on another GameObject that is active in the scene. Pass it a referece in the inspector to you inactive object, and it will work.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to set game objects as inactive via button 1 Answer

Multiple Cars not working 1 Answer

error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer 3 Answers

Load a GameObject that is outside of script and set it active at the same time 1 Answer

Distribute terrain in zones 3 Answers


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