Instantianig a teleporter prefabs with destination variables

Hi, I’ve made a basic teleport script for my game, and I’d like to use the teleporting “portals” as a magic spell in my game (create one, then create another somewhere further to be able to go back and forth instantly).

Here is my code for the teleporters:

using UnityEngine;

public class Teleporter : MonoBehaviour
{
	
    public bool teleported = false;
    public Teleporter destination;

    void OnTriggerEnter(Collider c) {
        if (c.CompareTag("Player"))
        {
            if (!teleported)
            {
                destination.teleported = true;
                c.gameObject.transform.position = destination.gameObject.transform.position;
            }
        }
    }

    void OnTriggerExit(Collider c) {
        if (c.CompareTag("Player"))
        {
            teleported = false;
        }
    }
}

This works just fine by dragging the first portal prefab to other’s destination in the inspector, and vice versa. However, for my magic spell, I need to instantiate the two portals. For the instantiated clones however, the destination variables of the Teleporter script are not assigned, and they won’t work, of course.
Just to be clear, here’s a video of the problem: http://www.youtube.com/watch?v=nfcUxn9DipM&feature=youtu.be

How would I go about adding the variables to the clones? There would only be two of these at the time to go back and forth with. Here’s the script I’m currently using to instantiate the portals (it’s in Javascript, but I guess that shouldn’t affect anything…?):

var portal : GameObject;
var portalDestination : GameObject;

function Update() {
    if(Input.GetKeyUp("p"))
        Instantiate(portal, transform.position, transform.rotation);

        if(Input.GetKeyUp("o"))
        Instantiate(portalDestination, transform.position, transform.rotation);
}

Any help would be appreciated, either Javascript or C# :slight_smile:

You should be use either C# or Javascript for both of these scripts. The two languages don’t like to communicate with one another, which means that you won’t be able to extract a ‘Teleporter’ reference from the instantiated objects the way you need to! The ability to translate between the languages will be very useful to you, if you learn it.

In any case, to ‘link’ the portals the way you want to, you need to use GetComponent or instantiate casting on the newly created instances.

public GameObject portal;
public GameObject portalDestination;
Teleporter startTele;
Teleporter endTele;

void Update() {
    if(Input.GetKeyUp("p"))
    {
        startTele = Instantiate(portal, transform.position, transform.rotation)) as Teleporter;
        if(endTele != null)
        {
            endTele.destination = startTele;
            startTele.destination = endTele;
        }
    }

    if(Input.GetKeyUp("p"))
    {
        endTele = Instantiate(portalDestination, transform.position, transform.rotation)) as Teleporter;
        if(startTele != null)
        {
            endTele.destination = startTele;
            startTele.destination = endTele;
        }
    }
}

This way, the two teleporters will automatically link to each other when they have both been created!