Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Javascript Array not converting to Actionscript

Discussion in 'Flash' started by Evil-Dog, Nov 16, 2012.

  1. Evil-Dog

    Evil-Dog

    Joined:
    Oct 4, 2011
    Posts:
    134
    Now that Unity 4 is out, I'm trying to convert my project to flash and I have this error for my usage of Array in Javascript
    Error: Implicit coercion of a value of type UnityScript.Lang:_Array to an unrelated type UnityEngine.Serialization:IDeserializable

    I would imagine basic dynamic arrays would be allowed. What should I use instead?
    I use Array, for instance, in a RandomHat class I made where I don't care what type goes in, only when it comes out I cast to the right type.

    Ideas about that people?
    Thanks
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    There's no good reason to use the JS Array class; if you need a dynamic array, use List (if it's of mixed types, make it a List of Object).

    --Eric
     
  3. Evil-Dog

    Evil-Dog

    Joined:
    Oct 4, 2011
    Posts:
    134
    Yeah ok, I've always used Array, guess I'll start using List, thanks buddy
     
  4. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,147
    I have got a similar error, so are the JS arrays not supported for flash?
     
  5. Evil-Dog

    Evil-Dog

    Joined:
    Oct 4, 2011
    Posts:
    134
    Like Eric said, gotta use list.
    Add import System.Collections.Generic;
    and use List.<Object> for multi type lists or more specific types when you know what type you'll have in your array, like List.<String>
     
  6. RalphH

    RalphH

    Unity Technologies

    Joined:
    Dec 22, 2011
    Posts:
    592
    Hey, this looks like a bug, please submit it using the bugreporter.
     
  7. adeward

    adeward

    Joined:
    Mar 1, 2013
    Posts:
    9
    Adding my voice to this, I'm getting build errors like this:

    Error: Implicit coercion of a value of type UnityScript.Lang:_Array to an unrelated type UnityEngine.Serialization:IDeserializable.

    just because I've declared a var type Array.

    It would have been nice if 1) Unity had warned me before buying my Flash Pro license that this sort of stuff was likely to happen, and 2) there was an authoritative source of information indicating what language constructs were (not) compatible with which target platforms.
     
  8. mtoivo

    mtoivo

    Joined:
    Jul 30, 2012
    Posts:
    274
    I had some issues with Arrays too, if I remember correctly. I ended up not using 'Array' type at all, just declaring variables as arrays like this:
    Code (csharp):
    1. private var cameras_s01 : GameObject[];
    Where cameras_s01 represets an array of cameras of the scene 01. Later, the variable is intialized like this:
    Code (csharp):
    1. cameras_s01 = new GameObject[6];
    If this workaroud works for you, it should compile to Flash also quite nicely.
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    If you need a dynamically-sized array, use a generic List. In most cases it's completely trivial to switch from Array to List. There's no reason to use Array (I wish UT would just make it obsolete), and your code will be better for having switched to List or built-in arrays, regardless of Flash.

    --Eric
     
  10. Ninh

    Ninh

    Joined:
    Aug 6, 2013
    Posts:
    5
    Hi,
    I have got a same problem when trying convert my project to Flash.
    Here is my code which using Array :
    Code (csharp):
    1.  
    2. var Spawn01 : GameObject;
    3. var Spawn02 : GameObject;
    4. var Spawn03 : GameObject;
    5. var Spawn04 : GameObject;
    6. var Spawn05 : GameObject;
    7. var Spawn06 : GameObject;
    8. var Spawn07 : GameObject;
    9. var Spawn08 : GameObject;
    10. var SpawnPoint : Array;
    11. var SpawnDelay : int = 0;
    12. function Start () {
    13. SpawnPoint =     [Spawn01.transform.position,
    14.                   Spawn02.transform.position,
    15.                   Spawn03.transform.position,
    16.                   Spawn04.transform.position,
    17.                   Spawn05.transform.position,
    18.                   Spawn06.transform.position,
    19.                   Spawn07.transform.position,
    20.                   Spawn08.transform.position];
    21. }
    22.  
    23. function Update () {
    24. if(SpawnDelay >0)
    25.     {
    26.     SpawnDelay--;
    27.     }
    28. if (SpawnDelay == 0)
    29.     {
    30.     SpawnDelay += 600;
    31.     for (var i : int = 0; i < 8; i++)
    32.             {
    33.             Instantiate(Zombie01,SpawnPoint[i%8],Quaternion.identity);
    34.             }
    35.     }
    36.  
    37. }
    38.  
    I'm a newbie, and i don't know how to use List Object in js. Somebody help me to translate this Array to List.
    Thanks.
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You should never use tons of separate variables like that, just use a built-in array to begin with. Also variables should be lowercase.

    Code (csharp):
    1. var spawnPoints : Transform[];
    --Eric
     
  12. Ninh

    Ninh

    Joined:
    Aug 6, 2013
    Posts:
    5
    I got it. It should be set into Vector3[] type.

    Thank you, Eric!