Passing character controller objects around between scripts

I have a script attached to the First Person Player controller that wants to pass the First Person Player Controller as a target to a script attached to another Game Object. I tried passing “this” but the compiler couldn’t figure that out. So I did GameObject.Find(“FirstPersonController”) and it passes that just fine. BUT, when I try to get the position of the object using target.transform.position I get 0,0,0 regardless of where the player controller is.

For now I’ve switched to main camera transform, but I really want to write generic scripts that my AI can use without a player and that can be used later when I make the game multi-player. I notice that when I run the game it instantiates a First Person Controller that is separate from the one in the editor (I assume it’s just another instance - not sure, it shows up in the Hierarchy).

So what am I missing? Is it just the wrong scripting language or is there some magic that happens when First Person Controller instantiates itself? Any suggestions on how to do a “target = this” equivalent here?

You need GetComponent(ComponentType)

“this” refers to the current component, which would be your attached script.

Thanks, that is half the battle. If I do GetComponent(transform) and pass that in I should have the correct transform at least. But I still won’t have the correct game object information which is what I really want. I wish there were a GetParentObject() or GetRootObject() we could use. Right now I look all of them up with Find(). But as I said, Find() on the First Person Controller returns the wrong instance of the object and the transform of that instance is always 0,0,0.

Judy

There is a Transform.parent variable which should help you and its not readonly so through this you can also assign the parent manually
Transform.root returns the topmost gameobject in the hierarchy.

If you passed the transform to the other script, you can access the GameObject of that transform by just using “.gameObject”

For example:

var player : Transform;
playerGameObject = player.gameObject;

You could of course, just pass the .gameObject instead of passing the transform, though, by passing “this.gameObject”.