dontdestroyonload is not working

I have this script attached to the fist player controller so i can save the position values between scenes. The problem is that it creates duplicates and it does not pass the correct last position of the FPC. This is the script:

var player : GameObject;
private var lastPos : Vector3[];
private var levelToLoad : int;
private var canLoadLevel : boolean = false;

function Awake () 
{
    if(gameObject.tag == "player" )
    {
        Destroy(this.gameObject);
        }
        else
        {
        DontDestroyOnLoad(transform.gameObject);
    lastPos = new Vector3[Application.levelCount - 1];
    }
}


function Update () {
    if (canLoadLevel) {
       lastPos[Application.loadedLevel] = player.transform.position;
       Application.loadLevel(levelToLoad);
        canLoadLevel = false;
    }
}

function OnLevelWasLoaded(level : int) {
    player.transform.position = lastPos[level];
}

Please someone help

you may be best storing the Vector3 in a static variable. put an empty game object in the scene with a manager script on it. this manager script put the dont destroy on load and store any variables you need in this script.

public static var lastPos : Vector3;

function Awake(){
DontDestroyOnLoad(transform.gameObject);
}

then just access this script when you wish to save the vector3 or load from it, this gameobject can just stay hidden in the scene then.

I have try but the problem is that i always get a lots of instances and i can not remove then by script…