• 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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Alayna · May 09, 2012 at 06:14 PM · gameobjectdisabletoggleenable

Help With Disabling/Enabling single GameObject

Hey, Im using a fairly cludge setup to turn on and off an object with a keypress and I was wondering if someone could help me refine it. Im certain that there are many overlaps with these two scripts that are unnecessary. However whenever I change this setup it never seems to work, I heard that it may have something to do with SetActiveRecursively () But I couldn't get that method to quite work either.

This first script is placed on the object to be toggled disabled/enabled.

 function Update () {
 if (Input.GetKeyDown("p")) {
    gameObject.active = !gameObject.active;
    }
 }

This script is placed on the first person controller.

 var onoff : boolean;
 var testObject : GameObject;
 
 function Update () {
 if(Input.GetKeyDown("p")){
     if (onoff == true)
         testObject.active = true;
     if (onoff == false)
         testObject.active = false;
 }
 }

Im very new to scripting and they appear to do the same thing, but they do not work without eachother. This is probably due to referencing an object that is disabled and one of the scripts being disabled when toggling. I would really appreciate some help with this matter please.

Comment
Add comment · Show 2
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 Lo0NuhtiK · May 09, 2012 at 06:18 PM 0
Share

since they're both using the same key and the same object is the target, why not just get rid of the script you have on the object and only use the one that's on your player?

avatar image Alayna · May 09, 2012 at 06:26 PM 0
Share

Unfortunately when I do this the object will only turn on from an off state and will not disable with the second keypress.

1 Reply

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

Answer by Chimera3D · May 09, 2012 at 06:17 PM

When you disable the object the script doesn't work anymore because the script is disabled to. You need to attach the script to something else.

(edit): Just attach your second script to the fps and make sure you've assigned the variable to the object in the inspector and it should work. No need for the first script. In addition you're not telling the onOff variable to turn on and off, otherwise it's just always false. Try saying if onOff = true, then gameObject.active = false and onOff = false;. Then instead of saying if onOff = false, just say else. Also make a start function and make onOff = true;

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 Alayna · May 09, 2012 at 06:23 PM 0
Share

I just tried what you recommended and (since the object starts hidden) when I press 'p' the object turns on, but when I press it again it wont turn off?

avatar image Lo0NuhtiK · May 09, 2012 at 06:27 PM 0
Share

mix the idea you had from your first script with the 2nd script....
e.g.


    var t : GameObject ;
    function Update(){
       if(getAkey)
       t.active = !t.active ;
    }
avatar image Alayna · May 09, 2012 at 06:54 PM 0
Share

I managed to get it to work by using your suggestions and changing the onOff variables thank you!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to disable an array of projectors 2 Answers

enable scripts/ components on other gameobjects for a different gameobject? 1 Answer

Enable/Disable Game Object With GUI Button 1 Answer

how to perform undo action in unity? 2 Answers

Alternatives to GameObject.SetActive(...) 2 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