Hello all,
I’m having an issue with a script that will respawn me to the nearest checkpoint if I trigger it. I am getting an error on line 33, 65 that says:
Assets/_scripts/PitTrigger.cs(33,65): error CS0120: An object reference is required to access non-static member `UnityEngine.Transform.position’
My guess it is is looking for a position on the axis’s but the book I’m following says this should work. Did I type it wrong?
using UnityEngine;
using System.Collections;
public class PitTrigger : MonoBehaviour {
void OnTriggerEnter2D(Collider2D collider)
{
if (collider.tag == "Player") {
GameObject trigger = GetNearestActiveCheckpoint();
if(trigger != null)
{
collider.transform.position = trigger.transform.position;
}
}
else{
Destroy(collider.gameObject);
}
}
GameObject GetNearestActiveCheckpoint()
{
GameObject[] checkpoints =
GameObject.FindGameObjectsWithTag ("Checkpoint");
GameObject nearestCheckpoint = null;
float shortestDistance = Mathf.Infinity;
foreach (GameObject checkpoint in checkpoints) {
Vector3 checkpointPosition = checkpoint.transform.position;
float distance =
(checkpointPosition - Transform.position).sqrMagnitude;
CheckPointTrigger trigger =
checkpoint.GetComponent<CheckPointTrigger>();
if(distance < shortestDistance && trigger.isTriggered == true)
{
nearestCheckpoint = checkpoint;
shortestDistance = distance;
}
}
return nearestCheckpoint;
}
}