Search Unity

C# random array int[]

Discussion in 'Scripting' started by eanjo7, Jul 7, 2014.

  1. eanjo7

    eanjo7

    Joined:
    Jun 1, 2014
    Posts:
    42
    Hi pls help!

    I want to simply random my array! but it has errors.

    take a look:

    public string[] magictKind = {"jetPack" , "magnet" , "bike" , "big" , "bunny" , "plane" };

    function Start() {
    // I want to random it but it has an error
    magictKind = magictKind[ Random.Range( 0 , magictKind.Length ) ];

    }

    what is the correct way? Im a javscripter so I dont know how to do it correctly in c#

    pls help tnx
     
  2. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    What's the error?
     
  3. eanjo7

    eanjo7

    Joined:
    Jun 1, 2014
    Posts:
    42
    hmm find the solution.


    void RandomizeStringArray( string[] arr )
    {
    for (int i = arr.Length - 1; i > 0; i--) {
    var r = Random.Range(0,i);
    var tmp = arr;
    arr = arr[r];
    arr[r] = tmp;
    }
    }