Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Nick V · Jan 25, 2010 at 09:11 PM · erroriphonejavascript-specificbce0019

Why am I getting "X" is not a member of "Y" on Unity iPhone, when it works regular Unity?

Hi, I've imported a few scripts into Unity iPhone which use GetComponent, like this line here:

cursorShip.GetComponent(Cursor).ChangeColors ();

It works fine in Unity 2.6, but in Unity iPhone it gives me this error:

'ChangeColors' is not a member of UnityEngine.Component.

What the heck's going on here? Thanks!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image duck ♦♦ · Jan 26, 2010 at 11:32 PM 0
Share

Edited title and tags to be more generic, because this problem is not limited to GetComponent, but appears when moving any dynamic typed scripts to Unity iPhone

1 Reply

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by duck · Jan 25, 2010 at 09:24 PM

That's because Javascript in Unity iPhone forces you to use strictly typed variables, rather than having Unity dynamically change variable types for you behind the scenes.

"Cursor" is a Component. In general, components do not have a "ChangeColors()" function, and the "GetComponent" function returns a value whose type is "Component".

When you're working in regular Unity's Javascript, and you try to access your Cursor component's "ChangeColors" function, Unity helps you by automatically changing the variable's type from "Component" to "Cursor". It does this behind the scenes, and this is called "dynamic type casting". It can do this, because "Cursor" inherits from "Component".

This is one of the niceties that you have to forsake in cold harsh world of iPhone development! With Javascript in Unity iPhone, you have to specifically tell Unity what type of variable you're talking about. To use GetComponent to do what you want, you need to do this:

var cursor : Cursor = cursorShip.GetComponent(Cursor);
cursor.ChangeColors();

And in general, when developing for the iPhone in Javascript, it's a good idea to add this line to the top of each script:

#pragma strict

This forces unity to alert you to any cases where you're not using strictly typed variables, so that you can systematically iron out all these kind of bugs :)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nick V · Jan 25, 2010 at 09:28 PM 0
Share

Interesting, thanks for letting me know. :)

avatar image duck ♦♦ · Jan 25, 2010 at 09:44 PM 0
Share

you're welcome! (you can click the tick to mark the answer as 'accepted')

avatar image Eric5h5 · Jan 25, 2010 at 11:00 PM 0
Share

Actually, it's only necessary to do "var cursor : Cursor = cursorShip.GetComponent(Cursor);" for the first line.

If you want to do the whole thing in one line, you can use "(cursorShip.GetComponent(Cursor) as Cursor).ChangeColors();"

avatar image duck ♦♦ · Jan 27, 2010 at 09:50 AM 0
Share

Updated snippet - removed the unnecessary 'as Cursor'.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Scripting errors only when building for iPhone 2 Answers

I got a major question 1 Answer

Why is this player script not working 1 Answer

Different behavior on ipad and iphone 0 Answers

Unity to Xcode Problem 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges