Referencing Other GameObjects

I’m having a problem referencing other gameObjects in my script.

I have some tanks with turrets.
I have parts named, vehicle1, turret, gun, etc.

So I was referencing these parts using Find() as suggested in the docs, but it’s not working.
The problem is that it seems to be finding all similarly parts named across all units, as opposed to just those parts that are children of a specific vehicle.

For example:
Each unit hierarchy is as such: Vehicle > Turret > Gun
So to reference the “Gun” object inside a vehicle that the script resided in:

var turret : GameObject;
var gun : GameObject;

turret = this.gameObject.Find(“Turret”);
gun = turret.gameObject.Find(“Gun”);

My script worked perfectly when there was only one tank. As soon as I added a second, problems started appearing. After much trial and error, I THINK it is because of some kind of referencing conflict.

How do I reference a specific object that is a child of a another specific object?

I would have though that referencing would be something simple like:
Vehicle1.Turret.Gun.
The docs tell me to use Find() in order to reference an object by name, but it seems to be getting the first entity named “Gun” across the whole scene, not just within “Vehicle1”

This can’t be right. But how else am I to reference these objects?

Find() is actually a static method, so it does not matter from which instance you call it, it will always search among all of the current scene’s GameObjects. To not get confused, I’d suggest to always call that method with GameObject.Find(…); so that you clearly see that it’s a static method.

Seeing the documentation, I think you’re actually looking for the Find() instance method of the Transform component (I see why this can be confusing now!).
So you should write:

turret = this.transform.Find("Turret");
gun = turret.transform.Find("Gun");

And it should give you the children of your current object!
According to the docs, this should work too:

gun = this.transform.Find("Turret/Gun");