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 Meater6 · Jan 04, 2013 at 10:02 AM · arrayconvertingpragma strict

Implicit Downcast Warning and Converting Arrays

Hello Unit Answers community!

I'm having an issue with converting arrays. I'm trying to convert a js array to a builtin array like so:

 var arr : Array = new Array();
 var someBuiltinArray : float[];
 
 arr.Push(0.5);
 
 someBuiltinArray = arr.ToBuiltin(float);

When I do this, and put pragma strict in the script, I get a warning: "WARNING: Implicit downcast from 'System.Array' to 'float[]'." I think I know why this is happening (the function ToBuiltin is returning a js array instead of builtin array, and pragma strict points this out to me). I looked in the docs and this was how it was done, minus the #pragma strict part.

What I want to know is, is there a way how to do this without dynamic typing? I think there should there should be, but the answer eludes me. I know simply removing the pragma strict will make the warning go away, but that is missing the point.

Thanks for any and all responses! :D

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
1
Best Answer

Answer by Eric5h5 · Jan 04, 2013 at 07:17 PM

You should not ever use the Array class. For dynamically-sized arrays, use a generic List (should have "import System.Collections.Generic;").

 var myList = new List.<float>();
 myList.Add (0.5);

You can often just leave it at that, since List is much faster than Array and is type-safe, but if you need to convert to a built-in array, then do:

 someBuiltinArray = myList.ToArray();

Anyway, the warning about downcasting is just that, a warning. It's not an error. You can use #pragma downcast to suppress the warning.

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 Meater6 · Jan 05, 2013 at 05:48 AM 0
Share

Thanks, very helpful! I will try to replace all my non-builtin arrays to lists. Also useful to know what pragma downcast does, I've been having trouble finding the definitions and purpose of all the pragmas; they are not in the unity docs, but are only available in Unity (at least that's what google says xD). Thanks again!

avatar image Eric5h5 · Jan 05, 2013 at 06:05 AM 1
Share

There are 3 as far as I know: #pragma strict, which you already know, #pragma downcast, which prevents downcast warnings, and #pragma implicit, which allows implicit declaration of variables without using "var" (generally not a good idea to use that one; it's only there in case you need compatibility with non-#pragma strict code).

avatar image
1

Answer by Setzer22 · Jan 04, 2013 at 11:22 AM

Hmm... I have to say I have no idea how the builtin arrays in Js work. But assuming you can access each sepparate element of that array and iterate through them, and know the size of the array (what a shame if an array can't do that, actually xD), you could use a very simple loop like this.

 //This is pseudocoded
 //Where I use arr.Length use whathever builtin method or property you have to check
 //the array's length
 
 for(int i = 0; i < arr.Length; ++i) {
     someBuiltinArray[i] = arr[i]
 
 }
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 Meater6 · Jan 04, 2013 at 04:12 PM 0
Share

I can see this working for now, but it would be good to know if there are other work-arounds. Thanks for the fast response, though! You've been very helpful. I wonder why "ToBuiltin" returns a js array, seems to be counterintuitive...

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Pragma strict and arrays 1 Answer

Implicit downcast and ArrayList 1 Answer

Pragma Strict - Implicit Downcast from 'Object' to 'UnityEngine.Transform' 5 Answers

Problem With Javascript to Built-in Array 0 Answers

I cant Get the value of an Array 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