- Home /
SetActive(true); not working
Hi, I used this code to disable the child Star from the Parent Galaxy, but when I want to visit this star, I am unable to enable it. I have 10 Galaxy folders, each have 9 star folders, each star has 9 Planet folders, and each has 9 Satellite folders. If I don't disable each Star folder when I don't need them I will have a maximum of 7290 visible objects, slowing down the program.
The code I use to disable and also works is; p = 0 to 9
starFolder= GameObject.Find("Galaxies" + p + "/Stars");
if (starFolder) starFolder.SetActive(false);
The code I use to enable and does not work is; p = 0 to 9
starFolder= GameObject.Find("Galaxies" + p + "/Stars");
if (starFolder) starFolder.SetActive(true);
I can render the Mesh to false, but too much work for 7290 objects. Easier letting the folder Star and its children be disabled.
Is it possible that by setting SetActive to false, if(starfolder) is now returning false, as it's now inactive?
I'm not exactly certain how you're doing this but here is a basic rundown of how SetActive() works http://docs.unity3d.com/Documentation/ScriptReference/GameObject.SetActive.html Im guessing it has something to do with a parent not being active and so the child does not appear active. does that help?
The parent (Galaxy) is active, and will always be. I just don't need to see all the stars with its children when I don't need them.
You cannot find Inactive objects using GameObject.Find(string);.This is the problem here.... Once you set an object to SetActive(false); you cannot find using GameObject.Find(""); . Enable/Disable meshrenderer of the objects ins$$anonymous$$d of Active/Inactive.
Answer by Bunny83 · Nov 02, 2013 at 06:36 PM
O$$anonymous$$, anyway around that. That is what I was thinking too.
Find it before you set it inactive, then set it active again later.
Can you get game object without find? Otherwise I will have to find each game object on start and store them in a variable.
It'd end up a lot easier if you just disable the renderer on each object
starFolder= GameObject.Find("Galaxies" + p + "/Stars");
if (starFolder) starFolder.renderer.enabled = false;
edit: Never$$anonymous$$d, just realised that would only disable the star, and not any of the children
You'll have to do this to disable:
$$anonymous$$eshRenderer[] childRenderers = GetComponentsInChildren<$$anonymous$$eshRenderer>();
foreach($$anonymous$$eshRenderer m in childRenderers){
m.enabled = false;
}
and this to enable:
$$anonymous$$eshRenderer[] childRenderers = GetComponentsInChildren<$$anonymous$$eshRenderer>();
foreach($$anonymous$$eshRenderer m in childRenderers){
m.enabled = true;
}
thx, but the problem would be I have halo, flares, and particles that would still render and would make my other objects like lightning and rings be active which I have currently inactive. Also the scripts would also be active.
Answer by j-p-e-g · Nov 02, 2013 at 07:45 PM
Couldn't you tag all your stars e.g. "stars", and then use
GameObject[] starFolders = GameObject.FindGameObjectsWithTag("stars")
to store them in an array at game start?
No idea how the order of the list is determined but if that can be controlled somehow, you could use the index to access the folder you need.
Answer by adreamvoyager · Dec 01, 2013 at 06:52 AM
I believe the issue is that some of the game objects are not loaded at the start up. I get around this issue by creating a startup function that fires on the first update.
bool startUp = false;
Update() {
if (!statUp) onStartUP();
}
void onStartUp() {
someObject.SetActive(true);
startUp = true;
}
Answer by blenderblender · Oct 22, 2014 at 02:21 PM
It do not work because the objects is not Active
Your answer
Follow this Question
Related Questions
Array SetActive C# Unity 1 Answer
Set Bool in Inspector to true or False 2 Answers
Problems with save 1 Answer
Making coins reappear after a set time 1 Answer
physics.OverlapSphere colliders 1 Answer