2d game Teleporter works only as a single instance

so, I’ve created the code and it works ok for the single teleport and exit. but if I duplicate it and on the new version set the new exit… it still sends the player to the previous exit, which I think I understand why, but would really like a heads up on how to do this better. I’m completely new to C# and only had a basic knowledge of MEL and some simple windows scripting before this so I’m not much of a coder. So I’d really rather examples in C#, because JS or BOO will just confuse me more :stuck_out_tongue:

Second part to this, is WHY am I doing it this way? well after trying various other examples on the 100’s of websites I’ve now visited all weekend… none of the others worked because of one simple detail… the onTriggerEnter feature wouldn’t work with the 2D Rigid bodies for some odd reason… the circle or box collider seemed to give me nothing back when I tried to debug.log it… nada… just a blank stare… here’s the code.

using UnityEngine;
using System.Collections;

public class teleporter : MonoBehaviour
{
	//this is the exit object
	public Transform exit;

	//This is the player
	public Transform TeleportMe;

	//Checking player in telelporter
	bool inTeleporter = false;
	float TeleportRadius = 0.2f;

	//excluding all but telelporters in a mask
	public LayerMask whatIsTeleporter;

	//calling the animator to check the value later
	Animator anim;

	void Start () 
	{
		anim = GetComponent<Animator> ();
	}

	void FixedUpdate ()
	{
		//this turns on or off IN TELEPORTER value uses "onWallCheck" game object
		inTeleporter = Physics2D.OverlapCircle (TeleportMe.position, TeleportRadius, whatIsTeleporter);
		anim.SetBool ("Teleporter", inTeleporter);
	}

	//teleport if in the teleporter this way, because 2D rigid bodies weren't returning trigger
	void Update () 
	{
		if (inTeleporter) 
		{
			anim.SetBool ("Teleporter", false);
			TeleportMe.position = exit.position;		
		}
	}
}

Thanks in advance :slight_smile:

excellent, I did find that today and when I tried it initially it didn’t seem to work. however it works now and it also solves my other issue at the same time, while at the same time letting make the script far shorter… thanks :slight_smile:

using UnityEngine;
using System.Collections;

public class teleporter : MonoBehaviour
{
	//this is the exit object
	public Transform exit;

	//This is the player
	public Transform TeleportMe;


	//teleport if in the teleporter this way, because 2D rigid bodies weren't returning trigger
	void OnTriggerEnter2D(Collider2D other)
	{
			TeleportMe.position = exit.position;		
	}
}