Why don't my objects appear in different places

Hi there, I’ve put this code together to have a gameobject appear in different places on startup but it always appears in the same place 0,6,0 Why is this? Thanks.

#pragma strict
     
    function Start() {
    
    PlayerPrefs.SetInt("jump_position", Random.Range(0, 4));
    
    if
    (PlayerPrefs.GetInt("jump_position", 1))
    {
    transform.position = Vector3(0, 2, 0);
    }
     
     if
    (PlayerPrefs.GetInt("jump_position", 2))
    {
    transform.position = Vector3(0, 4, 0);
    }
     
     if
    (PlayerPrefs.GetInt("jump_position", 3))
    {
    transform.position = Vector3(0, 6, 0);
    }
    }

Because you’re checking GetInt as it was a boolean and not integer, so it’s TRUE for each statement including the latest and being latest it’s what you get. You probably wanted to check for

switch (PlayerPrefs.GetInt("jump_position")) {
case 1:
   transform.position = Vector3(0, 2, 0);
   break;
case 2:
   transform.position = Vector3(0, 4, 0);
   break;
case 3:
   transform.position = Vector3(0, 6, 0);
   break;
}

The latest parameter of GetInt is the default value that is returned by GetInt when the pref is not defined in PlayerPrefs.

The second parameter in PlayerPrefs.GetInt() is not a value to match. It is the default value that should be returned if the key is not found. So you code should read:

 if (PlayerPrefs.GetInt("jump_position", 1) == 1)
 {
     transform.position = Vector3(0, 2, 0);
 }

But there is no reason to call PlayerPrefs() repeatedly. Here is some alternate code:

#pragma strict

function Start() {
        var i : int = Random.Range(0,4);
        PlayerPrefs.SetInt("jump_position", i);
    
    switch (i) {
    	case 1: transform.position = Vector3(0, 2, 0); break;
    	case 2: transform.position = Vector3(0, 4, 0); break;
    	case 3: transform.position = Vector3(0, 6, 0); break;
    }
}

You could even simplify it more if you were to put your positions in an array.