How to use GetComponent on Unity iPhone?

Hi, I'm working on the artificial intelligence of my game and looking for reference for iPhone/iTouch safe scripts.

When I try to use scripting from non-iphone source, I get an error for the command SimpleMove.

GetComponent (CharacterController).SimpleMove(direction);

It tells me SimpleMove is not a member of unityEngine.Component



GetComponent returns a "Component", not the component type you request. You then have to cast that component as the type you want to access its specific functions and members.


var other : ScriptName = gameObject.GetComponent(ScriptName);


ScriptName sn = gameObject.GetComponent<ScriptName>()

Your code looks like javascript so it should read:

var myController : CharacterController = GetComponent (CharacterController);

Using Javascript on the iPhone means all your scripts have an invisible "#pragma strict" attached to them, and have no ability to use dynamic typing.

(GetComponent(CharacterController) as CharacterController).SimpleMove(direction);