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 /
avatar image
0
Question by Kitan13 · Mar 27, 2017 at 08:16 PM · transformparentchildgameobjectsfind

Why GameObject.Find() work and parent.transform.Find() doesn't work?

I'm doing a enemy that has 3 parts, so when it takes 25% damage the tail will be destroy, but i'm using GameObject.Find("SharkTail") to find the tail and destroy. One of the problem with this code is, if i'm putting 2 enemies and make 25% damage to one, the code will find all the "SharkTail" in hierarchy that has the name and destroy them. So, i want to take the parent and find the child that called "SharkTail". Now, the real problem is that, when I use the parent.transform.Find("SharkTail").gameObject this give me a null exception. when I make 25% damage. So, I don't know why the GameObject.Find("SharkTail") works and parent.transform.Find("SharkTail").gameObject doesn't work.

Hierarchy
Object Inspector of Shark Head
Code1 Work
Code2 Doesn't Work
Code3 Parent of GameObject

Note: I'm doing all math of damaged in GameObject "SharkHead" so thats why im finding the parent and trying to find the childs again to work on instances of this gameObject.
PS. I don't know much English, so, sorry if there are grammar mistakes.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tanoshimi · Mar 27, 2017 at 08:25 PM

Your Code 2 doesn't work, because "GameObject" is not a type of component, so can't be retrieved using the GetComponent method. It is however a property of the transform component, so instead of:

 parentShark.transform.Find("SharkTail").GetComponent<GameObject>()

use

 parentShark.transform.Find("SharkTail").gameObject
Comment
Add comment · Show 6 · 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 Kitan13 · Mar 27, 2017 at 08:52 PM 0
Share

Also used that way and gives me null exception.

avatar image Kossuranta Kitan13 · Mar 28, 2017 at 07:26 AM 0
Share

Are you sure that your parentShark is assigned correctly, maybe print its name to console just to be sure? I just tried this in my game and it works just fine:

 print(transform.Find("Net").gameObject.name);

Anyway can't you just assign the parts in inspector, is there a reason why you need to find them during run time? Assu$$anonymous$$g that your shark doesn't grow new parts and same goes for the parent object as I guess it will always stay the same too, so you could just assign it in inspector.

avatar image Kitan13 Kossuranta · Mar 28, 2017 at 12:08 PM 0
Share

Well i think i solve the problem by doing your suggestion. I create two public variables typeOf GameObject: tailPart and chestPart. Then I assign the parts in the inspector, and now works, even if I put 3 or more sharks, only destroy the part that supposed to destroy, ignoring other gameObjects.

THAN$$anonymous$$S!!!

avatar image dvandamme · Mar 27, 2017 at 09:38 PM -1
Share

You've got a referencing issue. GameObject has a find function. You can access the GameObject owner of a transform (transforms always instantiate as part of a game object into the game world, and transforms always have a property reference to their own gameObject).

so you have transform.gameObject.Find("thingByName")

this will return this gameobjects child with the name, unless its not found, and then it will return a null reference error

if parentShark is typeOf Transform, then use

     parentShark.gameObject.Find("SharkTail")

to get a gameobject called SharkTail, that is a a child object of the parentShark object

avatar image Hellium dvandamme · Mar 28, 2017 at 10:27 AM 1
Share

The Transform class has a Find function : https://docs.unity3d.com/ScriptReference/Transform.Find.html Its purpose is to find a child of the given transform

The static Find function of the GameObject class will find any gameobject in the scene : https://docs.unity3d.com/ScriptReference/GameObject.Find.html

avatar image dvandamme Hellium · Mar 28, 2017 at 10:36 AM 0
Share

well well well... transform does have find.. will edit my comment

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

6 People are following this question.

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

Related Questions

Make a simple tree 1 Answer

Object scale/rotation changes when parented to flipped object 0 Answers

undoing parenting of object's transform 1 Answer

Instantiate a Prefab as child 0 Answers

Can I limit a .Find to the Parents and Child and not the Scene? 3 Answers


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