Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by IgnorantPizza · Aug 06, 2012 at 07:34 PM · teleportrigibody

Making a Prefab retain a variable

Hey, I'm new to unity, and java in general. I was wondering if there was anyway you could make a prefab retain a variable. The reason being is that currently I'm working on a portal-like game, where you shoot a portal, and it connects to the other portal. This is the portal code I'm using:

 using UnityEngine;
 using System.Collections;
 
 public class TeleporterFree : MonoBehaviour
 {
  public GameObject destination;
  public GameObject action;
  public float activateTime = 0.2F;
  
  void OnTriggerEnter(Collider other)
  {
  StartCoroutine(OnTeleport(other));
  }
  
  IEnumerator OnTeleport(Collider other)
  {
  GameObject.Instantiate(action, transform.position, transform.rotation);
  
  yield return new WaitForSeconds(activateTime);
  
  other.gameObject.transform.position = destination.transform.position+new Vector3(0, other.bounds.size.y, 0);
  other.gameObject.transform.rotation = destination.transform.rotation;
  }
 }

Right now I attached this to a cube, and added a rigibody to the cube. I also used the fps tutorial rocket launcher script and attached the cube to the rocket launcher script, but the portal loses the destination variable, and because of that, it doesn't work. Help and/or advise would be appreciated. Thanks

Comment
Add comment
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
0
Best Answer

Answer by whydoidoit · Aug 06, 2012 at 07:45 PM

Just a couple of points first: it's JavaScript or Unity Script and not Java (they are completely different languages) and your code is in C# in any case.

Prefabs can't have a reference to a scene object - it wouldn't make any sense because you can use that prefab on any scene and the reference wouldn't be valid.

I would suggest instead you have the portals as scene objects and connect them up in that way - if they aren't a part of the same scene then it won't work that way and you will need to devise your own method of loading the scene and referring to the object.

You might for instance use the name of the GameObject:

    public string destinationName;
    public GameObject destination;

    void Awake() 
    {
         destination = GameObject.Find(destinationName);
    }

If the object might have been created after the start of the game then this perhaps:

    public string destinationName;
    GameObject destinationObject;
    public GameObject destination
    {  
         get
         {
             return destinationObject = destinationObject ?? GameObject.Find(destinationName);
             
         }
     }



If you were additively loading your scenes it would be more tricky - perhaps like this:

    public string destinationName;
    public string destinationScene;
    GameObject destinationObject;
    public GameObject destination
    {  
         get
         {
             var dest = destinationObject ?? GameObject.Find(destinationName);
             if(dest == null)
             {
                  Application.LoadLevelAdditive(destinationScene);
                  dest = GameObject.Find(destinationName);
             }
             destinationObject = dest;
             return dest;
 
         }
     }

Comment
Add comment · Show 1 · 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 IgnorantPizza · Aug 06, 2012 at 08:15 PM 0
Share

Thanks for your time, that worked.

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.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Simplist way to rotate camera 180degrees on trigger? 1 Answer

Instantianig a teleporter prefabs with destination variables 1 Answer

Falling off respawn script 1 Answer

Asteroids in C#: Hyperspace (Teleport to random position) 1 Answer

How to teleport the player to the location of an instantiated projectile? 1 Answer


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