Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
-1
Question by zharko92 · Dec 09, 2017 at 06:00 AM · gameobjectchildassign

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!

Comment
Add comment · Show 17
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mbilalbark · Dec 09, 2017 at 07:07 AM 0
Share

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

avatar image zharko92 mbilalbark · Dec 09, 2017 at 04:09 PM 0
Share

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

avatar image Hellium · Dec 09, 2017 at 10:05 AM 0
Share

Doesn't your code work? $$anonymous$$ake sure monster_weapon_01 is a direct child of the transform (not a grand child)

avatar image zharko92 Hellium · Dec 09, 2017 at 04:12 PM 0
Share

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.

avatar image Hellium zharko92 · Dec 09, 2017 at 04:15 PM 0
Share

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

Show more comments
avatar image ShadyProductions · Dec 09, 2017 at 10:56 AM 0
Share

Try

 var children = parentTransform.GetComponentsInChildren<Transform>().ToList();
 weapon_1 = children.Find(f => f.name == "monster_weapon_01").gameObject;
avatar image zharko92 ShadyProductions · Dec 09, 2017 at 04:31 PM 0
Share

Doesnt work :S - The name 'parentTransform' does not exist in the current context

avatar image ShadyProductions zharko92 · Dec 09, 2017 at 04:51 PM 0
Share

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.

avatar image hexagonius · Dec 09, 2017 at 05:54 PM 0
Share

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.

avatar image zharko92 hexagonius · Dec 09, 2017 at 06:06 PM 0
Share

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)

avatar image Remy_Unity zharko92 · Dec 11, 2017 at 09:35 AM 1
Share

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.

Show more comments
avatar image Raimi · Dec 10, 2017 at 09:21 PM 0
Share

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.

avatar image Hellium Raimi · Dec 10, 2017 at 09:45 PM 1
Share

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..

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by zharko92 · Dec 12, 2017 at 08:57 AM

@Remy_Unity and @Hellium have the correct answers, Thx! ^^

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Dec 09, 2017 at 07:18 PM 0
Share

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

avatar image remy_rm Hellium · Dec 09, 2017 at 10:39 PM 0
Share

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

avatar image zharko92 remy_rm · Dec 09, 2017 at 11:39 PM 0
Share

As Hellium said, there will be multiples objects named "monster_weapon_01", but in different game objects..

Show more comments
avatar image
-2

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

107 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Store Rotation values of a Child in a Vector3 or Quaternion ? 1 Answer

Adding components vs child 0 Answers

Parent of RectTransform is being set with parent property. Consider using the SetParent method instead, with the worldPositionStays argument set to false. 1 Answer

Can not access GetComponentInParent ( ). usegravity 1 Answer

How can i assign a GameObject to another but only have some of its components? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges