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 Aug749 · Jul 24, 2013 at 12:44 PM · disappeargetkeyupreappear

Trying to get a GUI game object to disappear/reappear

I'm trying to get a GUI game object disappear when i press a button and reappear when i let go of the button. My problem is the GUI disappears, but never reappears. Here is my code:

 function Update () {
     if(Input.GetKeyDown(KeyCode.LeftShift))
         gameObject.active = false;
     if(Input.GetKeyUp(KeyCode.LeftShift))
          gameObject.active = true;
 }

Any help at all is appreciated! I'm pretty sure there is something small im missing here.

Comment
Add comment · Show 1
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 Aug749 · Jul 24, 2013 at 12:49 PM 0
Share

unity now informs me that gameobject.active in obselete and reccomends other things to use

Assets/disapear.js(3,20): BCW0012: WARNING: 'UnityEngine.GameObject.active' is obsolete. GameObject.active is obsolete. Use GameObject.SetActive(), GameObject.activeSelf or GameObject.activeInHierarchy.

2 Replies

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

Answer by Jamora · Jul 24, 2013 at 01:11 PM

You need to put the disabling/enabling script in a different gameobject to control this behaviour. The Update method doesn't run in a disabled gameobject/script. So something like

 var go : GameObject;
 
 function Update(){
  go.SetActive(!Input.GetKeyDown(KeyCode.LeftShift));
 }

You set the gameobject you want to control in the inspector.

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 Aug749 · Jul 24, 2013 at 01:19 PM 0
Share

i understand what you are saying, but how would i create a new game object to control the behaviour

avatar image Aug749 · Jul 24, 2013 at 01:20 PM 0
Share

im just not sure how to properly identify it

avatar image Aug749 · Jul 24, 2013 at 01:28 PM 0
Share

alright thank you i was close to that i just couldn't get it working

avatar image BimSekai · Jul 24, 2013 at 01:28 PM 0
Share

If you just want a GameObject to disappear you can just disable the renderer.

 gameObject.renderer.enabled = false;

Give it a try, the gameobject will still be active and therefor execute its update functions

avatar image Aug749 · Jul 24, 2013 at 01:31 PM 0
Share

this is the final script, Gey$$anonymous$$ey ins$$anonymous$$d of Get$$anonymous$$eyDown

 var go : GameObject;
  
 function Update(){
  go.SetActive(!Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftShift));
 }
avatar image
0

Answer by Dave-Carlile · Jul 24, 2013 at 12:48 PM

Determine if the key is down, then set active to true if they key is not down.

 bool keyDown = Input.GetKeyDown(KeyCode.LeftShift);
 gameObject.SetActive(!keyDown);

You could also do this in one line, but not quite as clear for you...

 gameObject.SetActive(!Input.GetKeyDown(KeyCode.LeftShift));

I guess this is in C# and you're doing JavaScript. The second example should be the same syntax. For the first you can do this...

 var keyDown = Input.GetKeyDown(KeyCode.LeftShift);

Or something like that.

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 Aug749 · Jul 24, 2013 at 01:05 PM 0
Share
 function Update () {
     gameObject.SetActive(!Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftShift));
 }

it has the same problem, it doesn't reappear i will try the first one

avatar image Aug749 · Jul 24, 2013 at 01:07 PM 0
Share

the first one did not work either

avatar image rhys_vdw · Jul 24, 2013 at 01:31 PM 0
Share

Why did you accept the answer if it is wrong? Jamora's answer is correct.

avatar image Dave-Carlile · Jul 24, 2013 at 01:37 PM 0
Share

The answer isn't wrong. But since it didn't solve the problem directly, I accepted Jamora's in its s$$anonymous$$d.

avatar image rhys_vdw · Jul 24, 2013 at 01:50 PM 0
Share

I accepted Jamora's in its s$$anonymous$$d.

Somewhat confused about how you're accepting the answer when you didn't ask the question. :-S

With regards to this answer, it is the same logic as the code in the question, and therefore it has the same problem that Aug749 was asking about. It will never be called while the GameObject that the script is attached to is disabled, and can therefore only disable it.

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

19 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

Related Questions

Load GUI.TextArea 1 Answer

How would I make an object disappear after a set amount of time? 3 Answers

Why is it that Every time i get near My Ragdoll model (FBX) it disappears? 5 Answers

very flat fbx objects disappear in game view? 2 Answers

Character teleport himself when play animation. 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