Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
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
7

Answer by chetanisinanand · Mar 14, 2013 at 01:31 AM

GameObject.active is Obsolete in Unity 4, Rather Use: gameObject.SetActive (false); Or GameObject.activeSelf, GameObject.activeInHierarchy

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 MakeITeasy · Nov 11, 2013 at 12:47 PM 0
Share

@chetanisinanand i have this compile error says: Assets/$$anonymous$$ain_GUI.js(107,51): BCE0019: 'activeSelf' is not a member of 'UnityEngine.GameObject'.

my code is (javascript):

 GameObject.FindWithTag("House").activeSelf = false;

What's wrong with it? Thank you in advance.

avatar image Xnerdz · Apr 15, 2014 at 06:02 PM 1
Share

That's because "activeSelf" is readonly.

avatar image
3

Answer by Sebas · Jan 28, 2010 at 01:04 AM

I tried the following:

var onoff : boolean; var testObject : GameObject;

function Update () { if (onoff == true) testObject.active = true; if (onoff == false) testObject.active = false; }

The script works fine when attached to something like the First Person Controller. As testObject I used a sphere. When I attach the script to the same object which I want to disable, I got a small problem.. That is, if you disable the gameObject to which your script is attached, the script won't get called anymore and your re-enabling your onoff variable won't have any effect. Hope that makes sense.

One option I could think of is to parent your objects to an empty game object, attach the script there and simply disable the child. That should do the trick, but there might be more elegant solutions out there.

/edit

Another option would be suitable if you only want the objects to either be rendered or not rendered. You could enable/disable the renderer like this http://unity3d.com/support/documentation/ScriptReference/Renderer-enabled.html

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 EG-Hernandez · May 21, 2013 at 02:45 AM 0
Share

@Sebas, i'm having the same issue with some children. but i want to enable them after a collision, sense I have them inactive themselves at the start, but I can't find the script to call the child. How would I enable or activate the child from the parent?

avatar image domsson · Feb 09, 2016 at 11:08 PM 1
Share

It is worth noting that using active is deprecated. Ins$$anonymous$$d, SetActive() should be used, i.e. testObject.SetActive(true);

avatar image
2

Answer by Duke · Jan 28, 2010 at 01:12 AM

I assume you mean the gameobject won't re-enable when you set the script's onoff variable to true in the editor, and that when you referenced "toggle" in your Update function, you really meant to reference onoff.

I think what you are running in to is that when you set gameObject.active = false, Update() no longer gets called every frame, therefore your code to set gameObject.active = true will never run.

If you have a reference to your script object to set the onoff variable (script.onoff = true), could you just access the gameObject directly from there? (script.gameObject.active = true)

Comment
Add comment · Show 4 · 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 vished · Feb 16, 2011 at 11:33 AM 0
Share

when you set gameObject.active = false, Update() no longer gets called every frame, therefore your code to set gameObject.active = true will never run.

WTF? That's so stupid and counter intuitive! I just wasted the last 6 hours wondering why! So now what? Can you reference an inactive transform from an active one? How can I reference an object WITHOUT "find" (searching through every object in the game)??? THAN$$anonymous$$S

avatar image Ninjaoboy · Jul 10, 2011 at 02:58 AM 0
Share

you could make a variable for the object ex "var building : Transform;" then in script say building.active = false; or true; or how you had it befor but this script should be attached to something that will never be disabled.

avatar image Joshua · Jul 10, 2011 at 03:06 AM 0
Share

How is that stupid and counterintuative? You turn it of - it no longer runs.. sounds like expected behavior to me.

There are tons of alternative to using find. You could even establish the reference before run-time in the inspector.

avatar image Skaring · Jan 26, 2015 at 02:03 AM 0
Share

Can you tell me how to do that Joshua? I'm having the same problem as him.

avatar image
1

Answer by $$anonymous$$ · Jul 05, 2015 at 02:54 AM

The statement .active is now deprecated, use .SetActive(true/false) instead.

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 Khesan · Feb 23, 2010 at 09:45 AM

Remember that .active is deprecated now, and you need to use .enable instead.

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 Eric5h5 · Feb 23, 2010 at 11:01 AM 3
Share

.active is not deprecated for GameObjects. It's only deprecated for components, because you're supposed to use .enabled on them.

avatar image create3dgames · Nov 20, 2012 at 02:54 AM 0
Share

Precisely. As Eric5h5 says.

  • 1
  • 2
  • ›

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

How Do You Have Multiple High Scores For 1 GameOverScene? 1 Answer

How to determine bounding box of scene? 2 Answers

Trouble while trying to manipulate object. 2 Answers

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

Retaining GameObject data when switching scenes? 4 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