Question by
Wach3D · May 07, 2017 at 03:08 AM ·
gameobjecttransformpositionarrayscheckpoint
How to get the position of a gameObject based on its index in an array?
I am trying to make a checkpoint system. I have an array of "Checkpoint" gameObjects.
What I'm trying to do is make it so that when the player enters one of the checkpoint boxes, the script gets the position of the checkpoint box that the player collided with and stores the position in a Transform variable called "newRespawnPoint". If the player dies, the player's transform will be changed to the stored Transform of the last checkpoint box with newRespawnPoint.
My script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCheckpoint : MonoBehaviour {
public GameObject[] checkpoint = new GameObject[0];
public Transform newRespawnPoint;
void Start ()
{
}
void Update ()
{
//if player dies, player's transform = newRespawnPoint
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject == this.checkpoint[0])
{
Debug.Log("Entered index 0");
//get checkpoint[0]'s position and apply it to newRespawnPoint
//this.checkpoint[0].GetComponent<Transform>() = this.newRespawnPoint;
}
if (other.gameObject == this.checkpoint[1])
{
Debug.Log("Entered index 1");
}
if (other.gameObject == this.checkpoint[2])
{
Debug.Log("Entered index 2");
}
}
}
Thanks in advance! I'm not the best at scripting and always appreciate the help.
1.jpg
(212.7 kB)
Comment