how do i know where my instantiated object came from?

Ok so I have 3 GameObjects that are being used to instantiate objects. They are all called TargetSpawner_GO

I have the following script attached to them:

    function SpawnAnother(){
        var localObject : GameObject = Instantiate(myObject,transform.position, transform.rotation);
        localObject.GetComponent(moveTarget).speedLeft = myCounter + 2;
        localObject.GetComponent(moveTarget).speedRight = myCounter + (-2);
    }

When the instantiated object is killed I use the following script:

  var deathClock : float = 5.0;
    var deathTime : float = 5.0;
    var startDeathClock = false;
    function Update () {

    deathClock -= Time.deltaTime;
    //Debug.Log(deathClock);

        if(startDeathClock){
            if(deathClock < 0){
                Destroy(gameObject);
                var myObject = GameObject.Find("TargetSpawner_GO").GetComponent(SpawnTarget);
                myObject.SpawnAnother();
            }
        }

    }

    function KillMeNow(){
    deathClock = deathTime ;
    startDeathClock = true;

    }

Now my problem: How do I know where the instantiated object came from so I can spawn another one at the location where it was first instantiated from.

Right now they are all coming from the same object when they die.

You could create a variable, kept on the spawned instance, that stores which 'spawner' it belongs to.

On a script attached to the spawned instance (which I will refer to as 'scriptName'):

var spawnOrigin : GameObject;

In SpawnAnother(), when you create the object, you would:

localObject.GetComponent(scriptName).spawnOrigin = gameObject;

This sets the 'spawnOrigin' variable (on the spawned instance) to the GameObject that spawned it.

You would then use the spawnOrigin variable to SpawnAnother(), i.e.:

spawnOrigin.GetComponent(SpawnTarget).SpawnAnother();

Without your full code, I can't post a complete snippet, but hopefully you'll be able to piece it together. Any questions, please ask.

The easiest would be to store the position (and rotation) of the object in the Start function, and use it to spawn the object:

(I am not a native JavaScript speaker, so the syntax might be a bit off). The following must be in the same script as the function SpawnAnother in your code.

var initialPosition : Vector3;
var initialRotation : Quaternion;

function Start()
{
  initialPosition = transform.position;
  initialRotation = transform.rotation;
}

function SpawnAnother()
{
  var localObject : GameObject = Instantiate(gameObject, initialPosition, initialRotation);
  localObject.GetComponent(moveTarget).speedLeft = myCounter + 2;
  localObject.GetComponent(moveTarget).speedRight = myCounter + (-2);
}

(P.S. I am not sure why you had gameObject as parameter for instantiate - I am assuming here that you want to clone the attached object, so I replaced it with gameObject).

Just wanted to point out that this post still helped me 12 years later in 2022.
This is how my code ended up looking:

GameObject newUIbutton = Instantiate(UIButton, gameObject.transform.position, Quaternion.identity);

newUIbutton.GetComponent<UIButtonFunctions>().spawnOrigin = this.gameObject;

So UIButton is a prefab which has a script on it called UIButtonFunctions which has a public GameObject variable called spawnOrigin.

I instantiate UIButton by assigning a variable to it first (i.e. “newUIButton”) then access and assign spawnOrigin via referencing the newUIButton variable.

Hope this helps someone else struggling with this.