- Home /
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.
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
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.
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!!!
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
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
well well well... transform does have find.. will edit my comment