- Home /
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
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;
}
}
Your answer
Follow this Question
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