- Home /
Is there a specific technique you have to use to .SetActive() a child GameObject of a GameObject you created using Resources.Load?
I am experiencing a problem in making a child GameObject of another GameObject become visible by using .SetActive(true) on the child GameObject. As it is stored in a Resources directory the child object is not active. I then use Resources.Load to create the object. Visually that result is just fine. I see the newly instantiated object but I don't see the inactive child object (which is text). But when I try to SetActive that child (which I can find by FindWithTag on the parent then after that finding the child by parent.transform.Find() ) it will not become visible. I have created a GameObject by manually instantiating that GameObject and used the same technique at same point in code to try to turn on the presently-inactive child object. It becomes visible. To me, it looks like this problem occurs because I just instantiated that object using Resources.Load. Is there some special technique that is needed in some circumstances to cause a child to go active? If I keep running the program then later it becomes possible to make that child go active. I can't as of yet pin down exactly what happens to make that possible. That child is NOT set to be static. I am executing my instantiate and .SetActive calls within a MonoBehavior class but not within the script/class associated with the parent gameobject. This problem ring a mental bell with anyone? The only possible workaround I can think of at the moment is to detect that I need to have that child active and double the number of Resource entries I have with one entry being for that child being not active and the other being for that child being active and Resources.Load the right one.
Is your parent game object active? An inactive parent will make it so the children act as inactive, even if they are active.
The parent object is active. There are actually several parent objects. The top level GameObject is Everything which is active, not static. Under Everything is Standard Game which is active, not static. Under Standard Game is Black PIeces which is active, not static. Under Black PIeces is the parent object I referred to in my original post. It is active, the child I want to become active is under that parent. What is happening is that I have a turn-based game with 2 players. At the end of a player turn what I want to do is erase all GameObjects for the 2 players and recreate (I must have this functionality so I can load saved games). The error I see appears after I originally create the game then perform a game action which must cause a non-active text to become active and appear over a piece affected by the game action after the 1st player ends their turn. All GameObject for the 2 players are Destroyed and recreated (using instantiate(Resources.Load(""))). I know the destroy works and I can see all the just-created GameObjects. One of these objects must have it's inactive text field become active and show a status text over the object. I can in the Unity Editor at the point of the error shift to the Scene view and look at the GameObjects. Everything is active except that child GameObject. Programattically at the point of error I can get a reference to that child. Using isactiveInHierarchy I detect that I successfully made it visible. But it doesn't really become visible. .SetActive() is not 100% reliable. At the point of error I have also tried marking all the GameObjects under Black Pieces as not active. They don't disappear. I assume this is because they were just instantiate(Resources.Load())'ed.
Answer by Cuttlas-U · Sep 09, 2017 at 07:45 PM
hi; u can make a refrence when u are creating an object like this ;
GameObject Temp = Instantiate(object,pos,rotaition) as GameObject;
then u have the refrence to that object u created and u dont need to find it u can active any of its child like this :
Temp.transform.GetChild(0).gameObject.SetActive(True);
the above code active the first child of the instantiated object;
Thanks for the suggestion. If the problem originates with how I get the GameObject reference (i.e. the problem lies in usage of Find's) then that might solve the problem. I am already working on implementing the workaround I mentioned I might try in the original post. A third possible workaround is to stop putting the sometimes-active and sometimes-inactive text field in as a child and ins$$anonymous$$d start creating, tracking, placing, and making active/in-active it as a major class of object itself.
good . go try the ideas ill check this topic if u had any problems;
Answer by Must_I_have_a_name · Sep 11, 2017 at 12:10 AM
Ok, I'm getting the results I need. The excellent suggestion from Cuttlas-U was critical to getting a working solution. When you instantiate(Resources.Load()) and discover that gameobject.FindWithTag and/or gameobject.transform.Find() and/or .SetActive() suddenly don't work then what you should do is store what instantiate spits out on the left side immediately after you create your clone and use THAT when you try to do anything with it. Also, in my situation it helped to create the child object that I want to be visible sometimes and invisible sometimes as active in the Resource when you create the Resource directory entry and then when you want it to vanish to not use .SetActive(false) but instead localScale it to 0.0f,0.0f,0.0f (make it infinitely small). That way you don't care that .SetActive(false) isn't working anymore. I saw that trick mentioned by someone(don't remember who) in the questions/answers of the last few days. Good trick. IMHO, instantiate(Resources.Load()) can create a GameObject which at least in some contexts cannot be accessed successfully by FindWithTag or Find on the transform or be set active or inactive and child objects also cannot be found or be set to active or set to inactive.
glad to see u fixed it; if the awsner was helpfull u can accept it as correct :)
Your answer
Follow this Question
Related Questions
Resources.Load() What has to be in Resources Folder 1 Answer
Resources.Load too much on Android = crash. 0 Answers
Convert XML Deserializer from folder to resources 2 Answers
resources not loadable in bulid 1 Answer
Loading XML asset after build 0 Answers