Oh… Well I guess I’ll stick with what I’ve got before I try something new and possibly break something xD Anyway, the thing I’m trying to fix is something different now. The seed has been synced up between the players, and the start segment’s exits are also synced up. But then for some reason, it throws an IndexOutOfRangeException at this line of code: ```
GameObject newSegment = Instantiate(segmentPrefabList[int.Parse(seed[GameObject.Find(“Start”).GetComponent().counter].ToString())], exits[j]);
which is itself in this method:
```csharp
public void LateGenerateSegment(string seed)
{
for (int j = 0; j < exits.Length; j++)
{
if (exits[j].childCount == 0)
{
GameObject newSegment = Instantiate(segmentPrefabList[int.Parse(seed[GameObject.Find("Start").GetComponent<Seed>().counter].ToString())], exits[j]);
GameObject.Find("Start").GetComponent<Seed>().counter += 1;
}
}
}
which is called on the start segment prefab from the client. It is also called when the start segment on the client’s side generates the first three segments in the seed string. That’s when the error shows up: when the LateGenerateSegment method is called on the segments that are generated by the start segment. inhale okay I hope that makes sense. But just in case here are the scripts:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using JetBrains.Annotations;
using Unity.Netcode;
using Unity.Netcode.Components;
using UnityEngine;
public class Segment : NetworkBehaviour
{
[Header("Segment variables")]
public bool isStart;
public int rarity;
public GameObject wallPrefab;
public GameObject[] segmentPrefabList;
public Transform[] exits;
[HideInInspector] public bool isOverlapping;
[HideInInspector] public bool keepGenerating;
bool startedGenerating;
bool finishedGenerating;
int flickerNum;
private void Awake() {
NetworkManager.Singleton.OnClientConnectedCallback += OnClientConnected;
}
private void Start() {
flickerNum = UnityEngine.Random.Range(0, 10);
if (!isStart)
{
GenerateSegment();
if (GameObject.Find("Start").GetComponent<Segment>().keepGenerating)
{
LateGenerateSegment(GameObject.Find("Start").GetComponent<Seed>().seed);
}
}
}
void OnClientConnected(ulong clientId)
{
if (IsServer)
{
Debug.Log("Client " + clientId + " connected!");
PlayerController connectedClient = NetworkManager.Singleton.ConnectedClients[clientId].PlayerObject.gameObject.GetComponent<PlayerController>();
connectedClient.recievedString.Value = GameObject.Find("Start").GetComponent<Seed>().seed;
}
}
private void Update() {
if (isOverlapping)
{
Destroy(gameObject);
}
if (transform.Find("Light") != null)
{
if (Camera.main != null)
{
if (Vector3.Distance(transform.position, Camera.main.transform.position) >= 300)
{
transform.Find("Light").gameObject.SetActive(false);
}
else if (Vector3.Distance(transform.position, Camera.main.transform.position) < 300)
{
transform.Find("Light").gameObject.SetActive(true);
}
}
else//this is gonna get mixed up between the players
{
transform.Find("Light").gameObject.SetActive(false);
}
if (!transform.Find("Light").Find("Bulb").GetComponent<Light>().enabled)
{
transform.Find("Light").Find("Bulb").gameObject.SetActive(false);
}
else if (transform.Find("Light").Find("Bulb").GetComponent<Light>().enabled)
{
transform.Find("Light").Find("Bulb").gameObject.SetActive(true);
}
}
if (startedGenerating && GameObject.Find("Start").GetComponent<Seed>().worldSize < 1)
{
finishedGenerating = true;
}
if (finishedGenerating)
{
for (int i = 0; i < exits.Length; i++)
{
if (exits[i].childCount == 0)
{
Instantiate(wallPrefab, exits[i]);
finishedGenerating = false;
}
}
}
}
public void GenerateSegment() {
startedGenerating = true;
if (GameObject.Find("Start").GetComponent<Seed>().worldSize > -1)
{
for (int j = 0; j < exits.Length; j++)
{
regen:
if (exits[j].childCount == 0)
{
int randomNum = UnityEngine.Random.Range(0, segmentPrefabList.Length);
int randProb1 = UnityEngine.Random.Range(0, segmentPrefabList[randomNum].GetComponent<Segment>().rarity);
int randProb2 = UnityEngine.Random.Range(0, segmentPrefabList[randomNum].GetComponent<Segment>().rarity);
if (randProb1 == randProb2)
{
GameObject.Find("Start").GetComponent<Seed>().seed += randomNum.ToString();
GameObject.Find("Start").GetComponent<Seed>().worldSize -= 1;
GameObject newSegment = Instantiate(segmentPrefabList[randomNum], exits[j]);
}
else
{
goto regen;
}
}
}
}
}
public void LateGenerateSegment(string seed)
{
for (int j = 0; j < exits.Length; j++)
{
if (exits[j].childCount == 0)
{
GameObject newSegment = Instantiate(segmentPrefabList[int.Parse(seed[GameObject.Find("Start").GetComponent<Seed>().counter].ToString())], exits[j]);
GameObject.Find("Start").GetComponent<Seed>().counter += 1;
}
}
}
}
Player script:
using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;
using Unity.Netcode;
using UnityEngine.InputSystem;
using Unity.Collections;
public class PlayerController : NetworkBehaviour
{
public NetworkVariable<FixedString128Bytes> recievedString = new NetworkVariable<FixedString128Bytes>();
[Header("Player Variables")]
[Tooltip("How sensitive is the mouse look?")]
public float mouseSensitivity = 2.0f;
[Tooltip("How fast can the player move?")]
public float moveSpeed = 5.0f;
[Tooltip("How high can the player jump?")]
public float jumpForce = 8.0f;
[Tooltip("The player's flashlight")]
public GameObject flashlight;
[Tooltip("The map camera")]
public GameObject mapCamera;
[Tooltip("The main camera")]
public GameObject mainCamera;
[Header("Interaction variables")]
[Tooltip("What layers can the player interact with?")]
public LayerMask interactionMask;
[Tooltip("How far can the player be away from the thing they're trying to interact with?")]
public float maxDistance = 10.0f;
private float verticalRotation = 0f;
private Rigidbody rb;
private bool isLookingAtMap;
private GameObject[] playerCameras;
RaycastHit hit;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
if (GameObject.Find("Start").GetComponent<Segment>().exits[0].childCount == 0)
{
GameObject.Find("Start").GetComponent<Segment>().keepGenerating = true;
GameObject.Find("Start").GetComponent<Segment>().LateGenerateSegment(recievedString.Value.ToString());
}
}
void Update()
{
if (!IsOwner) return;
if (!isLookingAtMap)
{
HandleMouseLook();
HandleJump();
}
HandleInteractions();
playerCameras = GameObject.FindGameObjectsWithTag("MainCamera");
for (int i = 0; i < playerCameras.Length; i++)
{
if (playerCameras[i] != mainCamera)
{
playerCameras[i].SetActive(false);
}
}
}
void FixedUpdate()
{
if (!IsOwner) return;
if (!isLookingAtMap)
{
HandlePlayerMovement();
}
}
void HandleMouseLook()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity;
verticalRotation -= mouseY;
verticalRotation = Mathf.Clamp(verticalRotation, -90f, 90f);
transform.Rotate(Vector3.up * mouseX);
Camera.main.transform.localRotation = Quaternion.Euler(verticalRotation, 0f, 0f);
}
void HandlePlayerMovement()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 moveDirection = new Vector3(horizontal, 0f, vertical).normalized;
Vector3 moveVelocity = transform.TransformDirection(moveDirection) * moveSpeed;
rb.velocity = new Vector3(moveVelocity.x, rb.velocity.y, moveVelocity.z);
}
void HandleJump()
{
if (Input.GetButtonDown("Jump") && IsGrounded())
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
bool IsGrounded()
{
return Physics.Raycast(transform.position, Vector3.down, 6f);
}
void HandleInteractions()
{
/*if (Input.GetKeyDown(KeyCode.F))
{
if (!flashlight.activeSelf)
{
flashlight.SetActive(true);
}
else
{
flashlight.SetActive(false);
}
}*/
if (Input.GetKeyDown(KeyCode.Tab))
{
if (!isLookingAtMap)
{
isLookingAtMap = true;
mapCamera.SetActive(true);
transform.Find("Canvas").gameObject.SetActive(false);
Cursor.lockState = CursorLockMode.None;
}
else
{
isLookingAtMap = false;
mapCamera.SetActive(false);
transform.Find("Canvas").gameObject.SetActive(true);
Cursor.lockState = CursorLockMode.Locked;
}
}
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, maxDistance, interactionMask))
{
GameObject currentObject = hit.collider.gameObject;
if (Input.GetMouseButtonDown(0))
{
if (currentObject.name == "Door" && currentObject.transform.parent.localRotation != Quaternion.Euler(0, 90, 0))
{
currentObject.transform.parent.localRotation = Quaternion.Euler(0, 90, 0);
}
else
{
currentObject.transform.parent.localRotation = Quaternion.Euler(0, 0, 0);
}
}
}
}
}
Edit: I changed LateGenerateSegment(GameObject.Find("Start").GetComponent<Seed>().seed); to ```
LateGenerateSegment(GameObject.FindGameObjectWithTag(“Player”).GetComponent().recievedString.Value.ToString());
which fixed it. I mean, everything's flipped in the client's world, but I'll probably fix it xD