How to make a Teleporter Pad send Player to other random Teleporter Pad?
This Script works just fine if there's only 2 Teleporters in the scene, if i put a third Teleporter it will send the player to the first Teleporter, and the first two Teleporter won't send the player to the third one.
 public class Teleporter : MonoBehaviour
 {
     public int serialNumber;
     public float cooldown;
 
     void Update()
     {
         if (cooldown > 0)
         {
             cooldown -= Time.deltaTime;
         }
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Player" && cooldown <= 0f)
         {
             foreach (Teleporter teleporter in FindObjectsOfType<Teleporter>())
             {
                 if (teleporter.serialNumber == serialNumber && teleporter != this)
                 {
                     teleporter.cooldown = 1;
                     Vector3 position = teleporter.gameObject.transform.position;
                     position.y += 2f;
                     other.gameObject.transform.position = position;
                 }
             }
         }
     }
 }
 
              
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by DoTA_KAMIKADzE · Sep 16, 2015 at 11:29 PM
Well, obviously by using random teleport. Let me give you an example:
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Teleporter : MonoBehaviour
 {
     public int serialNumber;
     public float cooldown;
 
     void Update()
     {
         if (cooldown > 0)
         {
             cooldown -= Time.deltaTime;
         }
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Player" && cooldown <= 0f)
         {
             Teleporter[] allTP = FindObjectsOfType<Teleporter>();
             //considering the fact in your code - that there should be 1 more teleporter with same serial number as this one in order to teleport
             //you'll need some confirmation before randomizing
             //randomizing can be done in various ways,
             //like for example inside while loop or just by creating custom list of possible teleports or ...
             //anyway, if I were you I'd probably go with second one, so here you go:
             List<Teleporter> allowedTP = new List<Teleporter>();
             foreach (Teleporter teleporter in allTP)
             {
                 if (teleporter.serialNumber == serialNumber && teleporter != this) allowedTP.Add(teleporter);
             }
             if (allowedTP.Count > 0)
             {
                 int rndNum = Random.Range(0, allowedTP.Count - 1);
                 //and now the rest of your code
                 allowedTP[rndNum].cooldown = 1;
                 Vector3 position = allowedTP[rndNum].gameObject.transform.position;
                 position.y += 2f;
                 other.gameObject.transform.position = position;
             }
             else
             {
                 //no other tp with same serial number found
                 //you can put here some user notification or whatever
             }
         }
     }
 }
 
              Your answer
 
             Follow this Question
Related Questions
Is there a way to teleport player after countdown timer hits zero? 1 Answer
Making Portals 2 Answers
Need help with specifics in Random.Range 1 Answer
Spawn the next room by opening a door. 3 Answers
How to make RANDOM COLOR on shader 1 Answer