- Home /
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
Thanks
Daev
retaged (chagned tag from ai to component) because the post is not directly AI related. Is this considered good practice? I just became a bit unsure. What do you guys think?
I changed the title, since this question has nothing to do with artificial intelligence.
Answer by Michael La Voie · Mar 24, 2010 at 09:42 PM
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.
Javascript:
var other : ScriptName = gameObject.GetComponent(ScriptName);
C#
ScriptName sn = gameObject.GetComponent<ScriptName>()
Your code looks like javascript so it should read:
var myController : CharacterController = GetComponent (CharacterController);
myController.SimpleMove(direction);
Excellent. If it was helpful, please mark it as the accepted answer.
Yep this did the trick! Gotta modify it to fit your own variables and such but if you do that right it works 5/5
Answer by Eric5h5 · Mar 24, 2010 at 10:21 PM
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);
Your answer
![](https://koobas.hobune.stream/wayback/20220612130259im_/https://answers.unity.com/themes/thub/images/avi.jpg)