x


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# :)

more ▼

asked Jan 07, 2012 at 03:46 AM

Yanneyanen gravatar image

Yanneyanen
1 16 12 13

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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!

more ▼

answered Jan 07, 2012 at 03:53 AM

syclamoth gravatar image

syclamoth
18.4k 20 28 118

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/My_Assets/Scripts/Teleporter.cs:14)"

And there's "none" in the inspector in the destination slot..

Jan 07, 2012 at 07:30 AM Yanneyanen

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!

Jan 07, 2012 at 07:38 AM syclamoth

Would you mind 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.

Jan 07, 2012 at 07:52 AM Yanneyanen
 if(destination == null)
 {
     // there isn't a teleporter, don't bother trying
 }

Sorry for the slow reply, I didn't see your post.

Jan 11, 2012 at 06:44 AM syclamoth
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2904
x2114
x1373
x144
x61

asked: Jan 07, 2012 at 03:46 AM

Seen: 1322 times

Last Updated: Jan 11, 2012 at 06:44 AM