- Home /
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# :)
Answer by syclamoth · Jan 07, 2012 at 03:53 AM
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!
Hi, this works better than my script in a way that in a way that if I already have the two portals on the scene and instantiate a new one, the new one teleports to the corresponding one that's already there. If I delete the ones from the scene and instantiate new ones however, I get
"NullReferenceException: Object reference not set to an instance of an object Teleporter.OnTriggerEnter (UnityEngine.Collider c) (at Assets/$$anonymous$$y_Assets/Scripts/Teleporter.cs:14)"
And there's "none" in the inspector in the destination slot..
Well, that's not a problem with my script. You should add a few lines into the Teleporter script, which check to make sure that 'destination != null' before attempting to teleport. If the destination is null, you could change the special effect on the teleporter object to reflect the fact that it does not have a valid destination!
Would you $$anonymous$$d explaining how to do this? What does that mean exactly? It checks if the other portal is there to teleport to? Sorry, I'm still fairly new to scripting. I really appreciate the help.
if(destination == null)
{
// there isn't a teleporter, don't bother trying
}
Sorry for the slow reply, I didn't see your post.