- Home /
Client throwing error Argument out of range error but server working fine
Hi, I'm building a multiplayer game where the environment (made of gameobjects) is destroyed over time. However my code keeps throwing an ArgumentOutOfRangeException: Argument is out of range. Parameter name: index, with a bunch of libraries and things listed underneath but only on the client. As far as i understand it this error corresponds to the list being empty or out of range but i have checked for both (as you can see) and there doesn't seem like there should be a problem. Any help you guys can provide would be great.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking;
public class TerrainController : NetworkBehaviour {
[SerializeField]
private float timeBeforeStart = 0f;
[SerializeField]
private float timeBetweenDestroy = 5f;
[SerializeField]
private string tagName = null;
private List<GameObject> tiles;
private float counter = 0f;
private GameObject toDestroy = null;
private bool startTimeReached = false;
private void Start()
{
tiles = ArrayConverter(GameObject.FindGameObjectsWithTag(tagName));
Debug.Log(tiles.Count);
}
private void Update()
{
if (isServer)
{
counter += Time.deltaTime;
//get random character and send to clientRPC
if (counter >= timeBeforeStart)
{
startTimeReached = true;
}
if (startTimeReached)
{
if (counter >= timeBetweenDestroy)
{
counter = 0;
int listIndex = Random.Range(0, (tiles.Count - 1));
RpcDestroyTile(listIndex);
toDestroy = tiles[listIndex];
tiles.RemoveAt(listIndex);
Destroy(toDestroy);
if (tiles.Count == 0)
{
this.enabled = false;
}
}
}
}
}
[ClientRpc]
void RpcDestroyTile(int listIndex)
{
if (!isServer)
{
toDestroy = tiles[listIndex];
tiles.RemoveAt(listIndex);
Destroy(toDestroy);
if (tiles.Count == 0)
{
this.enabled = false;
}
Debug.Log("list count:" + tiles.Count + " - index:" + listIndex);
}
}
//Loop through an array to put into a list
public List<GameObject> ArrayConverter(GameObject[] array)
{
List<GameObject> list = new List<GameObject>();
for (int x = 0; x < array.Length; x++)
{
list.Add(array[x]);
}
return list;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612173923im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiate player cards copy online at particular positions 0 Answers
Unity networking tutorial? 6 Answers
Best way to choose Google Play Realtime Multiplayer Host? 0 Answers
how can I send a render texture over the network? 1 Answer
Unet Shooting, trying to send a command for object without authority? 0 Answers