Store Child Object in Var

Hey all,

I am trying to use this code to store a child object in a variable.

cannon1 = gameObject.transform.FindChild("TurretCannon").GameObject;

But it’s not working? Can someone tell me what I am doing wrong?

Thanks
Steve

1 Answer

1

the first problem I see is that
.GameObject;
should be
.gameObject;

Yap that's only issue, but it's also give error on editor when you comple your script.

what error are you getting?

I am not getting error, I said the code written by Mr. Steve is also give error at time of compiling if script assigned to any Gameobject. because transform doesn't have any property which denotes GameObject word, its have a .gameObject which return Object which are using the transform

Just noticed you aren't the question asker, sorry. Thought there was still an problem, but this change should have fixed that compilation error

Hey All, I tried using a lower case g but instead I get this error at run time. > NullReferenceException > PlayerObjectHealth.Start () (at > Assets/Scripts/PlayerObjectScripts/PlayerObjectHealth.js:29)