• 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 question was closed Jul 18, 2015 at 11:24 AM by SaraCecilia for the following reason:

Problem is not reproducible or outdated

avatar image
5
Question by SpoonyBard · Jan 27, 2010 at 11:42 PM · gameobjectscene

Enable/Disable objects, with script

Hi, I was wondering if anyone could give me some help:

I have a project, and will have two scenes - a "before" and an "after" world. The "after" world will have lots of different objects that I need to be able to turn on and off via script, because the user will be purchasing changes to the world.

Here's my quick test code, I thought it would work, but the item won't re-enable when I check the box in the inspector's script section.

var onoff : boolean;
function Update () {
    if (toggle == true)
        gameObject.active = true;
    if (toggle == false)
        gameObject.active = false;
}

Comment
Add comment · Show 3
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 me2000 · Jun 01, 2015 at 03:58 PM 0
Share

whats wrong with my script, the gameobject doesent reappear!!??

var Reflection : GameObject;

function Update (){ if(Input.GetButtonDown("Fire2")) { Reflection.active = false; }

 if(Input.GetButtonUp("Fire2"))
 {
     Reflection.active = true;
 }

}

avatar image create3dgames · Jun 02, 2015 at 06:59 PM 0
Share

gameObject.active is obsolete. Use gameObject.SetActive() or SetActiveRecursively(). Also as a general rule you should use camelCase for variable names.

avatar image choxmi · Dec 07, 2015 at 07:04 AM 0
Share

You can instantiate a clone from the object which you are going to setActive. And everytime you need to set active, again instantiate a clone. It works.

9 Replies

  • Sort: 
avatar image
0

Answer by alexmon27 · Feb 12, 2011 at 04:32 AM

I had this same issue with a guiTexture since you cannot use the renderer property... my easy fix was to make my GUITexture object a child of an empty game object. So the hierarchy was gui_textureUpdated > gui_texture. I attached my script to the empty game object (gui_textureUpdated) that looked something like this...

var isTextureVisible : boolean; var someTexture : GUITexture;

function Update() { if(isTextureVisible) someTexture.gameObject.active = true; else someTexture.gameObject.active = false; }

If you are using anything else that does use the renderer property, just use gameObject.enabled instead of .active. This should fix your visibility issues. Hope this helped :)

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
avatar image
0
Wiki

Answer by I_hate_c_sharp · Jun 27, 2014 at 03:35 PM

gameObject.SetActive(false);

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

Answer by Hotsaucehater · Oct 05, 2014 at 09:50 PM

Heres what I did for my pause menu:

 #pragma strict
 
 var showingMouse : boolean = false;
 static var pause : boolean = false;
 var menucomponet1 : Transform;
 var menucomponet2 : Transform;
 var menucomponet3 : Transform;
 var menucomponet4 : Transform;
 var centercross : Transform;
  
 function Start()
 {
     showingMouse = false;
     Screen.lockCursor = true;
     renderer.enabled = false;
     pause = false;
     Time.timeScale = 1;
     menucomponet1.gameObject.active = false;
     menucomponet2.gameObject.active = false;
     menucomponet3.gameObject.active = false;
     menucomponet4.gameObject.active = false;
     centercross.gameObject.active = true;
 }
  
 function Update()
 { 
     if(Input.GetKeyDown(KeyCode.Escape) && showingMouse == false)
     {
         showingMouse = true;
         Screen.showCursor = false;
         Screen.lockCursor = false;
         var pause = true;
         Time.timeScale = 0;
         menucomponet1.gameObject.active = false;
         menucomponet2.gameObject.active = false;
         menucomponet3.gameObject.active = false;
         menucomponet4.gameObject.active = false;
         centercross.gameObject.active = true;
         
         
     } else if(Input.GetKeyDown(KeyCode.Escape) && showingMouse == true)
     {
         showingMouse = false;
         Screen.showCursor = true;
         Screen.lockCursor = true;
         pause = false;
         Time.timeScale = 1;
         menucomponet1.gameObject.active = true;
         menucomponet2.gameObject.active = true;
         menucomponet3.gameObject.active = true;
         menucomponet4.gameObject.active = true;
         centercross.gameObject.active = false;
                    
     }

Attach this script to an empty gameObject, and drag the parts of your menu into the Transform boxes here:

alt text

I am using this for a FPS so the center cross box is for the Center of the screen crosshairs.


1.png (56.3 kB)
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
avatar image
0

Answer by Derek-Wong · Jan 26, 2015 at 03:56 AM

i used gameObject.SetActive(false) to do this.

say, if i have a pause panel and want to show it only when player pressed the pause button, i can pausePanelGameObject.SetActive(false) at Start() and pausePanelGameObject.SetActive(true) when pressed the button.

However, if you untick the gameobject in the inspector you will get error as it will not be loaded so you cannot use SetActive on it.

Comment
Add comment · Show 2 · 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 create3dgames · Jan 26, 2015 at 05:16 AM 0
Share

You need to have a separate game object to handle the object that needs to be enabled/disabled.

avatar image Derek-Wong · Jan 26, 2015 at 05:42 AM 0
Share

yeah exactly

  • ‹
  • 1
  • 2

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

14 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

Related Questions

Retaining GameObject data when switching scenes? 4 Answers

Save game objects to new scene 0 Answers

How to restart a "prefab'd" level? ;) 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How Do You Have Multiple High Scores For 1 GameOverScene? 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