- Home /
How do I find my inactive objects and set them active again?
I inactivated my game objects by finding the objects with certain tags and inactivating them. Now, in my game, I want the camera to move back to the collection of objects but those objects when I move my camera are still inactive but I want them active when I move my camera back.
I realized that I can't find my game objects with their respective tags because that only works for active objects. I tried using GameObject.Find() But I get a nullreference exemption error and I think the way I'm doing it right now is too layman/hard-coded.
So, I'm kind of stuck and need suggestions. So,How do I find my inactive objects and set them active again?
What I did was create a list of all my objects and declared them publicly.
E.g.
public GameObject Block;
Block =GameObject.Find("block");
Block.SetActive(true);
but I get a null reference error and besides, I don't think this is a good idea to do it for multiple objects.
Answer by Eowyn27 · Jan 15, 2014 at 04:40 PM
What I did to solve this problem was publicly declare the objects and then take the name of the object and setactive to true.
So, e.g.
public GameObject Block1;
Block1.SetActive(true);
Answer by chechoggomez · Jan 15, 2014 at 07:22 PM
You can use Resources.FindObjectsOfTypeAll
https://docs.unity3d.com/Documentation/ScriptReference/Resources.FindObjectsOfTypeAll.html
This function can return any type of Unity object that is loaded, including game objects, prefabs, materials, meshes, textures, etc. It will also list internal stuff, therefore please be extra careful the way you handle the returned objects. Contrary to Object.FindObjectsOfType this function will also list disabled objects.
Just make sure you are not using it intensively, since this operation can take long time.
Answer by Fabkins · Jan 14, 2014 at 09:21 PM
I answered your previous post. I tried the code before I sent it you and it was fine so not really sure the problem as long as you are setting the public declaration is done once and you are making the blocks active and deactive in the same file ( class).
no I have separate classes for making the objects active and deactive. Also, different scripts attached to different objects. I realized when I'm finding objects by tags, it doesn't work :( nor findobjectsbytype. They don't work on inactive objects in the scene.
I suggest you restructure your code so you have one static class for both making the objects active and inactive. That static class could then hold the single copy of the public block list.
Okay, just to see if things work, I did a horrible solution where I had publicly declared my objects and then I set e.g. block1.SetActive(true); I dragged my object to the block1 field in the inspector and it works. However, I noticed for one of my objects where I added an empty game object and that empty game object has children that I set initially inactive, turned active after navigating back to the complex of blocks view. I want those to be inactive.
Please read suggestion in other thread concerning using the camera culling masks.
I suggest you close this one, bad form having same subject in two threads.