Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by chunjai · Apr 11, 2011 at 10:05 AM · javascriptiosiphonetypestrong

Strongly Typed Javascript on iOS

My app just got riddled with errors when I went to build and run for iOS. I found out it was because of the lack of 'strongly typed javascript' via here. The errors all seem to be coming from wherever I use the 'GetComponent'. Here's how I'm doing it...

//declaring variable
var manager = GameObject.FindWithTag("TagName");
//calling function
manager.GetComponent(GameManager).someFunction;

How can I make this a strong type?

Comment
Add comment
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

2 Replies

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

Answer by Eric5h5 · Apr 11, 2011 at 10:39 AM

GetComponent returns Component, so it needs to be cast to the correct type. The easiest way is to use generics:

//declaring variable
var manager = GameObject.FindWithTag("TagName");
//calling function
manager.GetComponent.<GameManager>().someFunction;

You can also do it the "hard way":

manager.(GetComponent(GameManager) as GameManager).someFunction;
Comment
Add comment · Show 1 · 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 chunjai · Apr 12, 2011 at 03:16 AM 0
Share

hey thanks mate. this worked wonders.

avatar image
-1

Answer by Leepo 1 · Apr 11, 2011 at 10:07 AM

Add #pragma strict to the top of your file to make debugging easier and to force yourself to use strict typing.

Rule N1 is to never simply use a var anymore. Always declare it's variable, like so:

#pragma strict
var manager : GameObject = GameObject.FindWithTag("TagName");

Comment
Add comment · Show 2 · 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 Eric5h5 · Apr 11, 2011 at 10:33 AM 0
Share

This is completely wrong, sorry. Type inference takes care of this for you. Whether you use "var manager : GameObject =" or "var manager =", since FindWithTag returns GameObject, that's used to infer the type of manager. So it makes no difference at all. (This is the same in C#.) The actual problem is that GetComponent returns Component, which needs to be cast to the correct type.

avatar image chunjai · Apr 11, 2011 at 10:37 AM 0
Share

@Eric5h5, I may have solved with this: var someScript : ScriptName = GetComponent (ScriptName); someSCript.function; the error disappears but i have a lot to run through before i know it's solved. thoughts?

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

No one has followed this question yet.

Related Questions

How to save data on iOS? 1 Answer

Help converting javascript to ios iphone scripting 2 Answers

Zooming in and out on iPhone 1 Answer

Command line builds for iOS - pro only? 0 Answers

clang: error: linker command failed with exit code 1 (use -v to see invocation) 0 Answers


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