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 AndyF · Dec 20, 2010 at 10:37 AM · nullreferenceexceptionactivegameobject.finddeactivate

Activating a game object that is inactive through inspector

I've disabled some gameobjects through setting the tick box in the inspector for these objects to off. Problem is I don't know how to activate these objects when my application is running. I tried this:

gameobject = GameObject.Find("MyObject");

gameobject.active = true;

This causes a "NullReferenceException" as the call to GameObject.Find is returning NULL. Thus I cannot set the object to active. Can anyone help me activate these objects?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Rod-Green · Oct 04, 2011 at 11:18 PM

This will find all objects of any type even if disabled/inactive. This is slow. So really either use it for preprocessing/editor scripts or cache the results.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public static class GameObjectHelper
 {
     public static List<T> FindObjectsOfType<T>() where T : UnityEngine.Object
     {
         return new List<T>((T[])GameObject.FindObjectsOfType(typeof(T)));
     }
 
     public static List<T> FindObjectsOfType<T>(bool p_includeHidden) where T : UnityEngine.Object
     {
         if(!p_includeHidden)
             return FindObjectsOfType<T>();
         
         return new List<T>((T[])Resources.FindObjectsOfTypeAll(typeof(T)));
     }
 }
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 Brenden-Frank · Nov 11, 2012 at 06:39 AM 0
Share

The problem with this is that it returns more than simply scene objects of type.

For example, If I use this to get all objects by type GameObject, I also get prefabs that haven't been instantiated.

This becomes a problem when trying to do something like setting all inactive GameObjects to active.

avatar image
0

Answer by runevision · Dec 20, 2010 at 10:45 AM

GameObject.Find only searches for active GameObjects.

See this related question and answer for info on how to handle the problem: http://answers.unity3d.com/questions/14178/cannot-toggle-active-on-gameobjects-that-are-inactive

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 AndyF · Dec 20, 2010 at 10:54 AM 0
Share

Thanks. I've already looked at this answer. The problem is that my gameobjects have been deactivated through the inspector so the 'Find' to build my initial array fails as the objects start deactivated.

avatar image davedev · Dec 20, 2010 at 11:06 AM 0
Share

I'm not absolutely sure about this, but I think the point is that when you deactivate it in the editor its not available to the compiled application. So, if you want something in the project it needs to be activated in the editor and then manage active/deactive in your scripts.

avatar image
0

Answer by Zogg · Oct 05, 2011 at 03:06 AM

The simplest solution would be to create a variable myobject and to assign manually MyObject to it in the Inspector.

 public var myobject : Gameobject // Visible in the inspector, set it to MyObject
 
 ...
 
 myobject.active = true;
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 Rod-Green · Oct 05, 2011 at 04:51 AM 0
Share

This is fine for one or two objects. But in cases where you're creating editor tools or something that needs to evaluate existing objects this solution doesn't work. As it requires prior knowledge and user managed lists. Burdening the user with maintenance is a pretty bad idea.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Referencing children of an instantiated object. 1 Answer

Turing GameObjects On and Off again. 4 Answers

Unity Drops Var Object Assignments on Runtime 0 Answers

How to use if statement properly 1 Answer

Gameobject.find & my error NullReferenceException: Object reference not set to an instance of an object 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