BASIC TELEPORTING QUEST: ‘Teleport Player Random’ - 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.

I created a new array:

[SerializeField] Transform[] teleportTargets;

And I modified my TeleportPlayer method:

void TeleportPlayer()
{
    var randomIndex = Random.Range(0, teleportTargets.Length);
    var teleportTarget = teleportTargets[randomIndex];
    player.transform.position = teleportTarget.position;
}

I set the teleportTargets in the Inspector.

2 Likes

This was my solution below, I think less lines makes for slightly cleaner code.

[SerializeField] Transform[] teleporters;
    void TeleportPlayerRandom()
    {
        Transform teleporter = teleporters[Random.Range(0,teleporters.Length)];
        player.transform.position = teleporter.transform.position;
    }
1 Like

We can use fewer!

    void TeleportPlayer()
    {
        player.transform.position = teleportTargets[Random.Range(0, teleportTargets.Length)].position;
    }
1 Like

Doesn’t Array.Length return the total amount of values?
Couldn’t then targets[index] potentially return a NullPointerException or an ArrayIndexOutOfBoundsException?

Not sure if that is correct but here is my solution:

if (teleportTargets == null && teleportTargets.Length > 0) return;
TeleportPlayer(teleportTargets[Random.Range(0, teleportTargets.Length - 1)].position);

It depends if it is a design issue if it’s not been set up properly.
You could put out your own meaningful error or warning if so.
If it doesnt matter then great.

I believe you do have a bug in that code though…

I see the bug now @EddieRocks :sweat_smile: Weird how the my character still teleports to a random location :face_with_raised_eyebrow:

if (teleportTargets == null && teleportTargets.Length > 0) return;
// Should be
if (teleportTargets == null || !teleportTargets.Length > 0) return;

But say that we have an array of 5 targets, the teleportTargets.Length returns 5.
The array position is n-1 for the last element in the array which means that if Random.Range(0, teleportTargets.Length) returns a 5 then I assume we would get an IndexOutOfRangeException?

Whoops - Ok you had two bugs then as I hadn’t spotted that one!!

If you have a teleport with say 2 options I think you will find it never goes to the second/last one.

(Im assuming the code is still…

teleportTargets[Random.Range(0, teleportTargets.Length - 1)]

)
{Ints and floats work differently in Random.Range}

Double negative kinda things are hard to read so I’d swap this around || if = 0

I’ve tried explaining what I mean in code here: https://dotnetfiddle.net/jf13EL

Not sure what you mean by Ints and floats working differently in Random.Range, but when I try to access array[array.Length] I get an exception. Could be that Unity has some inbuilt error handling that automatically resolves issues such as these, I’ll have to try that out when I’m at home :sweat_smile:

I also agree that the !teleportTargets.Length > 0 smells a bit bad, will definitely update that to || teleportTargets.Length == 0. Thanks for the tip :smiley:

If you look up Unity’s Random.Range you’ll find that Floats are inclusive of the end range and Ints are not so you need to use teleportTargets.Length to make sure you get the last teleport.
Test each way with 2 teleports and one wont ever get to the 2nd one…

2 Likes
    void TeleportPlayerRandom()
    {
            player.transform.position = teleportTarget[Random.Range(0, teleportTarget.Length)].position;
    }
2 Likes