Teleporting gameobject on collision multiple times question.

Hey guys,

Im creating a script for a “horror” game and I am quite a noob.
My scripts purpose is to move an object to a different location on the map when the player collides with it using other.transform.position.

public class PickUpAndCount : MonoBehaviour {

public GameObject[ ] teleportPoint;

// Update is called once per frame
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == “PickUp”)
{
//other.gameObject.SetActive (false);
other.transform.position = teleportPoint [0].transform.position;
}
}
}

this is what I have so far. i can get the gameobject to teleport once to another empty game object.

I cant for the life of me figure out how to make this happen a second or a third time and I am trying to be able to collect 5 objects total.

If anybody could throw a few tips my way I would greatly appreciate any help.

Add a private field for which index of teleportPoint you’re on, and then increment it every time you teleport, looping back to zero when it passes the last one.

Any chance you could give me a bit of a demonstration for this? I have never tried to use arrays before today… :slight_smile:

Check out this page for a detailed tutorial: https://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx

The things most applicable in this case are:

int index = 5;
GameObject item6 = teleportPoint[index];

The number between brackets is the index to access.
“5” gets you the sixth element because arrays start counting at zero.
int numberOfTeleportPoints = teleportPoint.Length;Length indicates how many items are in the array.

Thank you so much!!
i appreciate the guidance.