- Home /
Typed component declaration returning null in iOS
OK, so I've spent the morning trying to work out why this works in Unity, but not in iOS. I realise iOS is typed, and looking at other examples the declaration I'm using is correct. When I run the code in iOS myController is null. Have I missed something?
var myController : SidescrollControl = this.gameObject.GetComponent(SidescrollControl);
if (myController.IsMoving())
Below is the output from xCode debug window: (Filename: /Users/cnelson/Dropbox/Documents/2012/Unity/MobileTest02/Assets/Standard Assets (Mobile)/Scripts/PlatformerPlayerAnimation.js Line: 53)
NullReferenceException: A null value was found where an object instance was required. at PlatformerPlayerAnimation.Update () [0x00025] in /Users/cnelson/Dropbox/Documents/2012/Unity/MobileTest02/Assets/Standard Assets (Mobile)/Scripts/PlatformerPlayerAnimation.js:61
Thanks in advance;)
I know iOS can be touchy about casting, maybe try explicitly casting SidescrollControl?
That's already being done, but it's not necessary. e.g.,
var myController = gameObject.GetComponent(SidescrollControl);
That will type myController as SidescrollControl, since that's what GetComponent returns in this case. It's not any different in iOS.
Answer by Eric5h5 · Oct 01, 2012 at 05:15 AM
If that's actually the line being referenced by the error, then it's null because there is no SidescrollControl component attached to that object. If you're 100% sure there is such a component, then you likely accidentally attached the script to another object that doesn't have that component.
Your answer
![](https://koobas.hobune.stream/wayback/20220613081713im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Strange Xcode Error for iOS? YS Bundle Null Exception 0 Answers
Smooth Follow 2D works but Null Reference Error Remains 1 Answer
LineRenderer not instantiating on iOS or Xcode, Works fine in editor and Unity Remote 0 Answers
iOS Javascript problem 0 Answers
Bullets sometimes flying through enemies in Unity2D 2 Answers