- Home /
Question by
Oblivinator101 · Dec 27, 2013 at 04:28 AM ·
How to get OnTriggerEnter to affect all objects using script
I am trying to create a checkpoint using a script with an OnTriggerEnter function. I am doing this by having the trigger check if the collider is tagged with "Trap" or "Checkpoint". If its a trap then it destroys and then respawns the player. If its a checkpoint then it changes the spawn point's transform to the position of the checkpoint gameobject. However, it only changes the transform of the checkpoint gameobject but doesn't affect any of the other objects using this script, so if I jump into a trap, the player still respawns at the starting location.
public GameObject Player;
public Transform spawnPoint;
private GameCamera gameCamera;
void Start ()
{
gameCamera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<GameCamera>();
spawnPoint = GameObject.Find("StartSpawn").transform;
}
void Update()
{
}
void OnTriggerEnter(Collider other)
{
if(collider.gameObject.tag == "Trap")
{
Destroy(other.gameObject);
SpawnPlayer();
}
if(collider.gameObject.tag == "Checkpoint")
{
spawnPoint = collider.gameObject.transform;
}
}
void SpawnPlayer()
{
gameCamera.SetTarget((Instantiate(Player, spawnPoint.position, Quaternion.identity)as GameObject).transform);
}
Comment
Best Answer
Answer by KellyThomas · Dec 27, 2013 at 04:51 AM
If you want all instances of this script to share a common value for spawnPoint
then you can declare it static
:
public static Transform spawnPoint;