- Home /
Question by
hamoo7dfuad · Feb 03 at 09:21 AM ·
c#playerlistreset-position
Reset position for all objects in a list
Hi, I have a code that works perfectly but the code is long I have written the same code for each player is there a possible way to make a list of players and don't need to write the code again, I'm a beginner.
public GameObject playerPos, playerPos2;
private Vector3 playerResPos, playerResPos2;
public GameObject restPos, restPos2;
private void Update()
{
playerResPos = restPos.transform.position;
playerResPos2 = restPos2.transform.position;
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Player")
playerPos.transform.position = playerResPos;
playerPos2.transform.position = playerResPos2;
}
Comment
Best Answer
Answer by Caeser_21 · Feb 03 at 01:53 PM
public GameObject[] PlayerPos;
public GameObject[] ResetPos;
private void Update()
{
private void OnTriggerEnter2D(Collider2D collision)
{
for (int i = 0 ; i < PlayerPos.Length ; ++i)
{
PlayerPos[i].transform.position = ResetPos[i].transform.position
}
}
}
This should work as long as you have the same number of objects in both the PlayerPos and ResetPos Array