Instantiate players in Photon. Error: Index was outside the bounds of the array.
I have a problem at the moment to instantiate the players in scene. I'm trying to get two players in different positions when loading the scene. But I get this error: IndexOutOfRangeException: Index was outside the bounds of the array.
What can be the problem? I understand the error which can come from the time of calling the array, but I´m a new user in unity. I hope you can help me
Thanks This is the code
public class Septup : MonoBehaviour
{
public static Septup SP;
public Transform[] posiciones;
public void OnEnable()
{
if (Septup.SP == null)
{
Septup.SP = this;
}
}
}
public class PlayerM : MonoBehaviour
{
public PhotonView PL;
public int aparecer;
void Start()
{
aparecer = Septup.SP.posiciones.Length;
PL = GetComponent<PhotonView>();
if (PL.IsMine)
{
jugador = PhotonNetwork.Instantiate("P1", Septup.SP.posiciones[aparecer].position, Septup.SP.posiciones[aparecer].rotation, 0);
}
}
}
Answer by Captain_Pineapple · May 01, 2020 at 10:53 AM
when you have an issue with an error always add the line the error occures in. Other than that your problem is that you have an array and you try to call The index of Length
. If you have an array with 2 elements the Length is 2 but the last element has the index 1. You simply have to subtract 1.
Your answer
Follow this Question
Related Questions
Adding a new GameObject to an Array without overriding the others 0 Answers
I get an error message every time i run this script? Any ideas? 0 Answers
Getting the element that has the same element number on another list ? 2 Answers
My Collider is not triggering the first time (PUN) 0 Answers
Unity [PUN] New Instantiated players cannot see previously instantiated players 0 Answers