• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Yanneyanen · Jan 07, 2012 at 03:46 AM · instantiateprefabvariableteleportportal

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

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image

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!

Comment
Lo0NuhtiK

People who like this

1 Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Yanneyanen · Jan 07, 2012 at 07:30 AM 0
Share

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..

avatar image syclamoth · Jan 07, 2012 at 07:38 AM 0
Share

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!

avatar image Yanneyanen · Jan 07, 2012 at 07:52 AM 0
Share

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.

avatar image syclamoth · Jan 11, 2012 at 06:44 AM 0
Share
 if(destination == null)
 {
     // there isn't a teleporter, don't bother trying
 }

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to store a prefab in a variable 1 Answer

setting the text of an instantiated prefab's child's guitext object 1 Answer

How to force a variable to prefab 1 Answer

Access prefab's variables after instantiation 1 Answer

[Urgent]Changing variables of an object AFTER instantiation 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges