- Home /
Question by
SuperCrow2 · Jun 11, 2020 at 08:56 PM ·
c#gameobjectunity 2dgameteleporting
Disable teleporter after being used
So, I know this is way wrong lol. After you use a teleporter, I want both to be disabled for 5 seconds before you can use them again. Teleporting works, thats the only thing I got to work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Teleporter1 : MonoBehaviour
//left side teleporter
//teleports to right side
{
[SerializeField] Transform Teleport2; //player teleports
void OnTriggerEnter2D(Collider2D col)
{
col.transform.position = Teleport2.position; //teleports to right side
this.GetComponent<Collider2D>().enabled = false; //disabled after its used
StartCoroutine("Enable"); //enabled again after 5 sec.
}
private IEnumerator Enable() //this enables it after 5 seconds
//to disable after an amount of time, change true to false
{
yield return new WaitForSeconds(5f);
this.GetComponent<Collider2D>().enabled = true;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Teleporter2 : MonoBehaviour
//right side teleporter
//teleports to right side
{
[SerializeField] Transform Teleport1; //player teleports
private void Start()
{
this.GetComponent<Collider2D>().enabled = false;
StartCoroutine("Enable"); //enabled again after 5 sec.
}
void OnTriggerEnter2D(Collider2D col)
{
col.transform.position = Teleport1.position; //teleports to left side
this.GetComponent<Collider2D>().enabled = false; //disabled after its used
StartCoroutine("Enable"); //enabled again after 5 sec.
}
private IEnumerator Enable() //this enables it after 5 seconds
//to disable after an amount of time, change true to false
{
yield return new WaitForSeconds(0.5f);
this.GetComponent<Collider2D>().enabled = true;
}
}
Comment
Answer by thealexguy1 · Jun 11, 2020 at 09:14 PM
Basically what your code is doing is disabling the teleporter that the player had just left, not the one they are arriving at. This means the player would just teleport there and back instantly. If you changed the Teleport1 and Teleport2 variables to type Teleporter1 and Teleporter2 (i.e. the scripts), you would be able to access the Enable function for both (if you make it public) when the player enters one of the teleporters.