How to get a child GameObject and assign it to a public GameObject variable?
The code should be something like this:
public GameObject weapon_1;
void Start()
{
weapon_1 = transform.Find("monster_weapon_01").gameObject;
}
??? "monster_weapon_01" is the name of a child GameObject of the GameObject who have this script
I really need to assign by code a child GameObject, Thanks in advantage!
Did you unity editor add prefab at weapon_1? And Where you define monster_weapon_01 game object? https://docs.unity3d.com/ScriptReference/Transform.GetChild.html
Did you unity editor add prefab at weapon_1? - No. - And Where you define monster_weapon_01 game object? - Here "public GameObject weapon_1;" - "monster_weapon_01" is the real name of the GameObject, and "weapon_1" is the name of the variable who I want to contain "monster_weapon_01", should be same thing
Doesn't your code work? $$anonymous$$ake sure monster_weapon_01
is a direct child of the transform (not a grand child)
Is not a direct child, is a child of a child of a child of a child (4 times xD) - Has to be a direct child? - is there any way to find and assign that child by code? Can you tell me how, please? Thanks.
If you are trying to find a grand child, you will have to implement a custom function. @Bunny83 made one here : https://answers.unity.com/questions/799429/transformfindstring-no-longer-finds-grandchild.html
Try
var children = parentTransform.GetComponentsInChildren<Transform>().ToList();
weapon_1 = children.Find(f => f.name == "monster_weapon_01").gameObject;
Doesnt work :S - The name 'parentTransform' does not exist in the current context
parentTransform is supposed to be the parent's transfrom you want to get the child from.. But if it is indeed not the direct child, this won't work. Hellium has the more correct answer. But perhaps you should learn some basic C# before you try unity.
Why do you want to assign it by code? you can just do so in the inspector, which is the actual procedure within an object hierarchy that stays the same all (most of) the time.
I know, I usually do in the inspector, really simple, but, this is a script from 1 enemy, I´m thinking in duplicate thousand of times, if I have to assign in the inspector every time ill get crazy x)
Indeed, assigning it in the inspector is probably the way to go.
Isn't your monster object a prefab ? If you have 1 prefab for the monster, asign the weapon in the prefab's inspector, and all monster instances in the scene will reference their respective weapons.
If you still want to stick with the find method, you will have to recurse in the hierarchy of your monster object to find the correct weapon object.
Don't know why my answer got 2 votes down. The question says...
How to get a child Gameobject and asign it to a Gameobject variable?
That's exactly what my answer does. If my answer is incorrect then you need to ask the correct question. You shouldn't vote people down if the question isn't well formed or detailed enough.
You had downvotes because the OP added information in the comments.
As Hellium said, there will be multiples objects named "monster_weapon_01", but in different game objects..
Answer by zharko92 · Dec 12, 2017 at 08:57 AM
@Remy_Unity and @Hellium have the correct answers, Thx! ^^
Answer by remy_rm · Dec 09, 2017 at 06:59 PM
You'll want to use weapon_1 = GameObject.Find("monster_weapon_01")
instead, Since you'll already be searching for GameObject this way you don't need to get .gameobject either
Because there may be multiple objects named monster_weapon_01
, this solution won't have the expected result since GameObject.Find
will return the first Gameobject named monster_weapon_01
Is there a script on the monster_weapon_01 that is unique to that object, or atleast unique inside the children structure? because if so then you could do weapon_1 = GetComponentInChildren<uniqueScriptName>().gameObject
As Hellium said, there will be multiples objects named "monster_weapon_01", but in different game objects..
Answer by Raimi · Dec 10, 2017 at 01:42 AM
GameObject childRef;
void Start()
{
childRef = GameObject.Find("ObjectName").transform.GetChild(0).gameObject;
}
https://docs.unity3d.com/ScriptReference/Transform.GetChild.html