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
5
Question by Fusobotic · Jul 01, 2010 at 01:56 PM · toggleactivegameobject.active

Cannot toggle active on gameobjects that are inactive

This problem is giving me headaches and is taking a while to figure out, can you guys help me out. I posted a related problem a few days ago that worked, but only for making objects inactive. Now that they are inactive, whenever I try to use the GameObject.FindGameObjectsWithTag() function, it cannot find the inactive gameobjects.

Here's my code:

static var time = 0.0; static var timerOn = false; static var exploring : boolean; static var zones : GameObject[];

function Start() { exploring = false; var zones = GameObject.FindGameObjectsWithTag("zones"); //var zones = GameObject.FindGameObjectsWithTag("zones"); //exploring = true exploring = true; }

function Update () { if (timerOn == true) { time = time+1*Time.deltaTime;

       //Debug.Log (time);
   }

if (exploring == false) { //for(var go : GameObject in GameObject.FindGameObjectsWithTag("zones")) for(var go : GameObject in GameObject.FindGameObjectsWithTag("zones")) { go.active = true; } } if (exploring == true) { for(var go : GameObject in GameObject.FindGameObjectsWithTag("zones")) { go.active = false; } } }

I've messed around with it quite a bit, so sorry for all the comments, and weird stuff in the start function.

Thanks for any help!

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
8
Best Answer

Answer by spinaljack · Jul 01, 2010 at 02:12 PM

What you need to do is maintain a list of inactive objects so you don't need to use find with tag all the time which is what your problem is.

When you deactivate them do something like this:

var objArray;

function Start(){ objArray = new Array; }

... your bit ...

if (exploring == true) { for(var go : GameObject in GameObject.FindGameObjectsWithTag("zones")) { objArray.Push(go); go.active = false; } }

Then when you want to bring them back on do this:

while(objArray.length>0){
   var go : GameObject = objArray.Pop();
   go.active = true;
}

Array docs:

http://unity3d.com/support/documentation/ScriptReference/Array.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 Fusobotic · Jul 01, 2010 at 03:04 PM 0
Share

Yes that works perfectly! Thank you!

avatar image spinaljack · Jul 01, 2010 at 07:34 PM 0
Share

Glad I could help

avatar image
1

Answer by Ippokratis · Feb 04, 2011 at 03:16 PM

Another approach would be to selectively disable the components that a gameObject have, instead of disabling the gameObject. This approach lets you access again the gameObject via the methods you mentioned: GameObject.FindGameObjectsWithTag()

Suppose that you have a gameObject with a Mesh Renderer and a script called myScript. You can set those two object to a disabled state:

renderer.enabled = false;
GetComponent.<myScript>.enabled = false; 

Colliders are more tricky. You can disable them by setting the gameObject in a different layer eg 10.

gameObject.layer = 10;

You should then define in the Physics inspector that objects in layer 10 don't collide with other objects. Physics inspector can be found in Menu->Edit->ProjectSettings->Physics and the place you define the collisions is the Layer Collision Matrix.

Notice that for perfomance reasons it seems to be a good practice to disable all scripts that contain onGui functions when you don't use them.

GetComponent.<myGuiScript>.enabled = false;

A good explanation on why you should do this is here http://www.mindthecube.com/blog/2010/09/avoiding-performance-cost-of-ongui

References:

http://unity3d.com/support/documentation/ScriptReference/GameObject-layer.html

http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponent.html

http://unity3d.com/support/documentation/ScriptReference/Behaviour-enabled.html?from=MonoBehaviour

(since scripts are MonoBehaviours that descent from Behaviours so they inherit their variables)

http://unity3d.com/support/documentation/ScriptReference/Renderer-enabled.html

Comment
Add comment · Show 1 · 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 valichm · Apr 07, 2012 at 03:39 PM 0
Share

I'm having trouble defining the collision parameters for a specific layer (layer 10 in your example) to turn off collisions... Could you elaborate?

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

No one has followed this question yet.

Related Questions

GUI Toggle Using Button Style Remain Active 1 Answer

Script only works onetime 0 Answers

GUI Button selected changes color and stays until another button is clicked 1 Answer

Toggling my minimap 1 Answer

How do I only show objects near the player 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