Search Unity

Updatting a variable in array without removing/adding

Discussion in 'Scripting' started by mattcscz, Dec 5, 2010.

  1. mattcscz

    mattcscz

    Joined:
    Mar 7, 2010
    Posts:
    411
    hi, is it possible to update a variable within an array thats called from a custom function?
    for example, i have a quest where you must run and pickup a crate and with my array i have built a quest tracker to show my progress which would say "crate 0/1" and when i pickup the crate i have amount++ but it wont update as i did arr.push from a custom function that isnt called again :S thanks!
     
  2. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Code (csharp):
    1. var myArray : Array = new Array();
    2. myArray.Push(1);
    3. myArray.Push(9000);
    4. myArray.Push(5);
    5.  
    6. myArray[0] = myArray[0] + 1;
    7. myArray[1] = 9001;
    8. myArray[2]++;
    EDIT: actually, I'm not sure with the ++ example with UnityScript (give it a shot!), but at the very least you can do like: myArray[0] = myArray[0] + 1
     
  3. mattcscz

    mattcscz

    Joined:
    Mar 7, 2010
    Posts:
    411
    awesome, much appriciate it man :D and i tried myarray[1] ++ it doesn't work but everything else does :D
     
  4. mattcscz

    mattcscz

    Joined:
    Mar 7, 2010
    Posts:
    411
    Strange though in a label i cant use myarray[0] = myarray [0] + 1 as it will treat at as "write to label myarray[0] 1 " so it would show 01. also if i do myarray[0] + 1 itll give me a error and if i do myarray[0] += 1 it will also show 01. However if i do myarray[0] = "" +1; it seems to clear it and show just 1 XD
     
  5. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    I'm not sure I'm quite following what it is you're doing. Can you post some code from your experiments?

    How are you assigning values into your array? Are they strongly typed as integers or are you passing in strings? (if you're passing in strings, then it will concatenate the values as text, not add them together like numbers)

    Perhaps you could also try to use a strongly typed list:
    Code (csharp):
    1. var myValues : List.<int> = new List.<int>();
    2. myValues.Add(9000); //"Add" instead of "Push" with the List class
    3. myValues[0] = myValues[0] + 1;
    If you use strongly typed collections, then it's pretty much impossible for you to accidentally mix and match data types.
     
  6. mattcscz

    mattcscz

    Joined:
    Mar 7, 2010
    Posts:
    411
    its fine they are working fine when i do myarray[0] = "" +1; this works fine, its just strange how the other examples i tried didn't :p its possibly becuse i have mutiple info in the the single slot.
     
  7. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Wait, you want your results to be "01"?
     
  8. mattcscz

    mattcscz

    Joined:
    Mar 7, 2010
    Posts:
    411
    no :p, my result came out as 01 when i used myarray[0] + 1 or myarray[0] = myarray[0] + 1. However when i used this "myarray[0] = "" + 1" it will come out as just 1. i would post my script but its spread throughout 5
     
  9. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    myarray[0] = "" + 1
    is the same as
    myarray[0] = "1" //note, this is a string, not an integer

    I have a feeling that the way you are initially assigning values are turning your numbers into text.
     
  10. mattcscz

    mattcscz

    Joined:
    Mar 7, 2010
    Posts:
    411
    haha probally, as said before, there are mutiple variables in my array [0] for labels, its starting to make more sense in my head ;)