Array not Working Properly

Hi All,

I have a problem that is probably very simple to fix. I have an array that is populated with 3 z values that control what lane the player is in (like Subway Surfers) but when I try to access one of those values randomly, It chooses a number somewhere in between these values, like 1.523… I need it to either choose -4, 0, or 4 on the Z axis and either 0 or 5 on the Y axis.

    public static int fZValue1 = -4;
    public static int fZValue2 = 0;
    public static int fZValue3 = 4;
    [Space(10)]
    public static int fYValue1 = 0;
    public static int fYValue2 = 5; 

    public int[] zValues = { fZValue1, fZValue2, fZValue3 };
    public int[] yValues = { fYValue1, fYValue2 };


    void Update ()
    {
        //Fuel
        fSpawnStartTimer -= Time.deltaTime;
        if (fSpawnStartTimer < 0.01 && GameInit.gameIsPlaying == true)
        {
            FuelSpawn ();
        }
    {

void FuelSpawn ()
    {
        Instantiate (fuelCan, new Vector3 (player.transform.position.x + fSpawnRange, Random.Range (0, yValues.Length), Random.Range (0, zValues.Length)), Quaternion.identity);
        fSpawnStartTimer = Random.Range (fSpawnRateStart, fSpawnRateEnd);
    }

Thank you! :slight_smile:

it’s becouse the vector3 is a float.
but the random.Range before the vector3

void FuelSpawn ()
    {
        int y = Random.Range (0, yValues.Length);
        int z = Random.Range (0, zValues.Length);

        Instantiate (fuelCan, new Vector3 (player.transform.position.x + fSpawnRange, y, z), Quaternion.identity);
        fSpawnStartTimer = Random.Range (fSpawnRateStart, fSpawnRateEnd);
    }
1 Like

Got it working, thanks!! :slight_smile: