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

Convert UnityScript Array to List for C# conversion

Discussion in 'Scripting' started by KrayonZA, Apr 14, 2015.

  1. KrayonZA

    KrayonZA

    Joined:
    Mar 5, 2013
    Posts:
    52
    Does anyone know how to convert the below arrays into a generic list?

    I'm banging my head against a wall I've been trying to get this working for 8 hours.
    Code (JavaScript):
    1. var upgradesApplied : boolean[];
    2. var upgrades : Upgrade[];
    3. private var storeUpgrades : Upgrade[];
    4.  
    5. function Awake() {
    6.     upgrades = Array(GetComponentsInChildren(Upgrade)).ToBuiltin(Upgrade) as Upgrade[];
    7.     var tempArr = new Array();
    8.     upgradesApplied = new boolean[upgrades.length];
    9.     for(var i = 0; i < upgrades.length ; i ++) {
    10.         upgradesApplied = upgrades.applied;
    11.         if(upgrades.showInStore){
    12.             tempArr.Push(upgrades);
    13.         }
    14.     }
    15.     storeUpgrades = tempArr.ToBuiltin(Upgrade) as Upgrade[];
    16. }
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I can't see how that would compile at all; stuff like "upgradesApplied = upgrades.applied" is a syntax error, since upgrades is an Upgrade[] array, and arrays don't have an "applied" property. Here's my best guess based on what seems more logical:

    Code (csharp):
    1. var upgradesApplied : boolean[];
    2. var upgrades : Upgrade[];
    3. private var storeUpgrades : List.<Upgrade>;
    4.  
    5. function Awake() {
    6.     upgrades = GetComponentsInChildren.<Upgrade>();
    7.     storeUpgrades = new List.<Upgrade>();
    8.     upgradesApplied = new boolean[upgrades.Length];
    9.     for (var i = 0; i < upgrades.Length; i++) {
    10.         upgradesApplied[i] = upgrades[i].applied;
    11.         if (upgrades[i].showInStore) {
    12.             storeUpgrades.Add (upgrades[i]);
    13.         }
    14.     }
    15. }
    --Eric
     
    KrayonZA likes this.
  3. KrayonZA

    KrayonZA

    Joined:
    Mar 5, 2013
    Posts:
    52
    Thanks Eric5h5

    The below worked for me, you were quite spot on, the guy from cShapatron gave me a hand its his tool i'm trying to use to convert my project to c#

    Code (JavaScript):
    1. var upgradesApplied : boolean[];
    2. var upgrades : Upgrade[];
    3. private var storeUpgrades : Upgrade[];
    4. function Awake() {
    5.     upgrades = GetComponentsInChildren.<Upgrade>();
    6.     var tempArr = new List.<Upgrade>();  
    7.     upgradesApplied = new boolean[upgrades.length];
    8.     for(var i = 0; i < upgrades.length ; i ++) {
    9.      upgradesApplied[ i ] = upgrades[ i ].applied;
    10.         if(upgrades[ i ].showInStore){
    11.             tempArr.Add(upgrades[ i ]);  
    12.         }
    13.     }
    14.     storeUpgrades = tempArr.ToArray();
    15. }