Feel free to share your solutions, ideas and creations below. If you get stuck, you can find some ideas here for completing this challenge.
For this challenge, I guess the solution that was expected was to disable the whole teleporter, you basically set the whole GameObject to SetActive(false) (basically disabling it).
I didn’t want to disable the whole Teleporter as it would basically disappear. I tried to SetActive(false) the BoxCollider component instead and discovered that SetActive was only available for GameObjects.
So I did a quick search in the documentation and here is what I came up with :
void OnTriggerEnter(Collider other)
{
TeleportPlayer();
DeactivateObject();
// Challenge 4: IlluminateArea();
// Challenge 5: StartCoroutine ("BlinkWorldLight");
// Challenge 6: TeleportPlayerRandom();
}
void DeactivateObject()
{
BoxCollider boxCollider = GetComponent<BoxCollider>();
boxCollider.enabled = !boxCollider.enabled;
}
I like this solution. I ended up disabling the whole teleporter with SetActive(false) and you’re right, I didn’t like how it suddenly disappeared!
My solution was to create a new field isActive
, which is true
by default.
[SerializeField] bool isActive = true;
void OnTriggerEnter(Collider other)
{
if (!isActive) return;
TeleportPlayer();
DeactivateObject();
}
void DeactivateObject()
{
isActive = false;
}
I tried doing enabled = false
, but apparently that only prevents the component’s Update
method from running; other methods contine to work, including OnTriggerEnter
.
That’s the code that I used for this challenge
void DeactivateObject()
{
BoxCollider boxCollider = GetComponent<BoxCollider>();
boxCollider.enabled = false;
}
I liked removing the object. Here’s the code I used.
void DeactivateObject()
{
BoxCollider boxCollider = GetComponent<BoxCollider>();
boxCollider.gameObject.SetActive(false);
}
I like this solution the most because it’s concise in the scope of the code in the file itself. The objects’ active state can be manipulated by outside factors but this script for itself will know when it’s not time to teleport.
void DeactivateObject()
{
BoxCollider boxCollider = GetComponent<BoxCollider>();
boxCollider.isTrigger = false;
}
I first used GameObject.SetActive as suggested but realised this would not work when creating a button or some floor trigger since it would delete the object entirely. I then tried disabling the boxCollider as some of you suggested but this removed the “physicality” of the object (ex: you can now walk inside the object since it doesn’t have an outer shell). I settled on removing the trigger from the teleport instead. Since it’s not a trigger, it will not “trigger” the code, but it will keep its physicality.
I did something similar here
void OnTriggerEnter(Collider other)
{
if (!isActiveAndEnabled) return;
TeleportPlayer();
DeactivateObject();
}
void DeactivateObject()
{
this.enabled = false;
}
But rather than creating a new boolean flag, I just used Unity’s built-in enabled property. This allows me to reactivate the teleport functionality from another script if needed.
Looks good!
First the code, then the explanation.
public class Teleport : MonoBehaviour {
// flag used to enable disable the teleporter
bool canTeleport = true;
void OnTriggerEnter(Collider other)
{
if (canTeleport)
{
// teleport player logic
InitializeCooldown();
}
}
void InitializeCooldown()
{
StartCoroutine("TeleportCooldown");
}
IEnumerator TeleportCooldown()
{
// disable ability to teleport.
canTeleport = false;
Debug.Log("Teleport on cooldown...");
// Set cooldown timer
yield return new WaitForSeconds(5f);
// Reinitialize teleport ability.
canTeleport = true;
Debug.Log("Teleport reactivated.");
}
}
Disabling objects is always a bit tricksy because you end up disabling all the scripts attached to it as well. Those scripts may end up doing things that you need the objects to be active for, so instead what I did was set up a simple flag to indicate whether or not the teleporter was on cooldown.
The cooldown in this case is a 5 second yield instruction that is called by the InitializeCooldown
method.
Happy Holidays everyone!
Really surprised at how many solutions there are to this challenge. I will add another to the list.
void DeactivateObject()
{
this.GetComponent().isTrigger = false;
}
Here is my solution:
First check if the teleport target is not null
void OnTriggerEnter(Collider other)
{
// Challenge 2: TeleportPlayer();
if (other.GetComponent<FirstPersonMovement>())
{
TeleportPlayer(other);
DeactivateObject();
}
}
void TeleportPlayer(Collider player)
{
if (teleportTarget)
{
player.transform.position = teleportTarget.position;
}
}
After that, once player got teleported, we set that teleport target to null
void DeactivateObject()
{
this.gameObject.GetComponent<Teleport>().teleportTarget = null;
}