BASIC TELEPORTING QUEST: ‘Deactivate Object’ - Solutions

Feel free to share your solutions, ideas and creations below. If you get stuck, you can find some ideas here for completing this challenge.

1 Like

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;
    }
3 Likes

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! :laughing:

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.

4 Likes

That’s the code that I used for this challenge

 void DeactivateObject()
    {
        BoxCollider boxCollider = GetComponent<BoxCollider>();
        boxCollider.enabled = false;
    }
1 Like

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.

1 Like