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#