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.
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’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
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
– mbilalbarkDoesn't your code work? Make sure
– Helliummonster_weapon_01is a direct child of the transform (not a grand child)Try var children = parentTransform.GetComponentsInChildren<Transform>().ToList(); weapon_1 = children.Find(f => f.name == "monster_weapon_01").gameObject;
– ShadyProductionsWhy 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.
– hexagoniusDon'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.
– Raimi