Server authoritative spawning bug?

I’m very new to UNET Networking so some insight into how I approached this would be greatly appreciated.

I’m trying to implement a server authoritative spawner that randomizes a prefab and a material before spawning them later into the scene. But for some reason from the client, I am seeing the right block spawn but with a different material (almost as if the number randomized again). I can’t seem to wrap my head around why this is happening, but a guess would be that the values are not synchronized (SyncVar)? If that is the case, then why are my prefabs not random as well?

Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class BlockSpawnOnline : NetworkBehaviour
{
    public GameObject smallBlock, regBlock, bigBlock, TBlock;
    public Material[] playerMaterials = new Material[4];
    public float spawnTime;
    
    int randomBlock;
    int randomMaterial;

    int maxBlocks = 10;
    float elapsed = 0;
    bool isSpawnBlocked;

    public List<GameObject> blocksToSpawn;

    public override void OnStartServer()
    {
        blocksToSpawn = new List<GameObject>();

        for (int i = 0; i < maxBlocks; i++)
        {
            randomBlock = Random.Range(0, 10);
            randomMaterial = Random.Range(0, 4);

            if (randomBlock <= 2)
            {
                blocksToSpawn.Add(smallBlock.gameObject);
            }
            else if (randomBlock <= 5)
            {
                blocksToSpawn.Add(regBlock.gameObject);
            }
            else if (randomBlock <= 7)
            {
                blocksToSpawn.Add(bigBlock.gameObject);
            }
            else
            {
                blocksToSpawn.Add(TBlock.gameObject);
            }
            blocksToSpawn*.GetComponent<MeshRenderer>().material = playerMaterials[randomMaterial];*

}
}

void Update()
{
if (isServer)
{
if (isSpawnBlocked)
elapsed = 0;
else
elapsed += Time.deltaTime;

if (elapsed > spawnTime && maxBlocks > 0)
{
Vector3 spawnPos = new Vector3(transform.position.x, transform.position.y + blocksToSpawn[maxBlocks-1].transform.localScale.y / 2, transform.position.z);
NetworkServer.Spawn(Instantiate(blocksToSpawn[maxBlocks - 1], spawnPos, transform.localRotation));
maxBlocks–;
elapsed = 0;

Debug.Log(this.name + " spawned " + blocksToSpawn[maxBlocks - 1].name + “, " + maxBlocks + " blocks left.”);
}
}
}

void OnTriggerStay(Collider collision)
{
if (collision.gameObject.tag == “Block”)
{
isSpawnBlocked = true;
GetComponent().material.SetColor(“_Color”, Color.red);
}
}

void OnTriggerExit(Collider collision)
{
if (collision.gameObject.tag == “Block”)
{
isSpawnBlocked = false;
GetComponent().material.SetColor(“_Color”, Color.yellow);
}
}
}

Never mind, I figured it out. I had to pass in the spawned object and the random number as parameters for my Rpc call after the object is spawned on the network, so that each client can see and grab the same object and change its material accordingly.

I feel like a lot of the change had more to do with deleting my playerMaterials array and just passing in each individual material publicly as I had done with my prefabs. I don’t know why this fixed my problem, but I’m assuming the order of the materials somehow changed in the building process? If anyone can enlighten me on that, that would be great.

Fixed Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class BlockSpawnOnline : NetworkBehaviour
{
    public GameObject smallBlock, regBlock, bigBlock, longBlock;
    public Material playerOne, playerTwo, playerThree, playerFour;
    public float spawnTime;
    
    int randomBlock;

    int maxBlocks = 10;
    float elapsed = 0;
    bool isSpawnBlocked;

    public List<GameObject> blocksToSpawn;

    public override void OnStartServer()
    {
        blocksToSpawn = new List<GameObject>();

        for (int i = 0; i < maxBlocks; i++)
        {
            randomBlock = Random.Range(0, 10);

            if (randomBlock <= 2)
            {
                blocksToSpawn.Add(smallBlock.gameObject);
            }
            else if (randomBlock <= 5)
            {
                blocksToSpawn.Add(regBlock.gameObject);
            }
            else if (randomBlock <= 7)
            {
                blocksToSpawn.Add(bigBlock.gameObject);
            }
            else
            {
                blocksToSpawn.Add(longBlock.gameObject);
            }
        }
    }

    [ServerCallback]
    void Update()
    {
        if (isSpawnBlocked)
            elapsed = 0;
        else
            elapsed += Time.deltaTime;
            
        if (elapsed > spawnTime && maxBlocks > 0)
        {
            maxBlocks--;
            Vector3 spawnPos = new Vector3(transform.position.x, transform.position.y + blocksToSpawn[maxBlocks].transform.localScale.y / 2, transform.position.z);
            GameObject spawnedBlock = Instantiate(blocksToSpawn[maxBlocks], spawnPos, transform.localRotation);
            NetworkServer.Spawn(spawnedBlock);
            RpcChangeBlockMaterial(spawnedBlock, Random.Range(0, 4));
            elapsed = 0;

            //Debug.Log(this.name + " spawned " + blocksToSpawn[maxBlocks].name + ", " + maxBlocks + " blocks left.");
        }
    }

    [ClientRpc]
    void RpcChangeBlockMaterial(GameObject newBlock, int randomNum)
    {
        Debug.Log(randomNum);
        if (randomNum == 0)
            newBlock.GetComponent<MeshRenderer>().material = playerOne;
        if (randomNum == 1)
            newBlock.GetComponent<MeshRenderer>().material = playerTwo;
        if (randomNum == 2)
            newBlock.GetComponent<MeshRenderer>().material = playerThree;
        if (randomNum == 3)
            newBlock.GetComponent<MeshRenderer>().material = playerFour;
    }

    [ServerCallback]
    void OnTriggerStay(Collider collision)
    {
        isSpawnBlocked = false;
        if (collision.gameObject.tag == "Block")
        {
            isSpawnBlocked = true;
            GetComponent<MeshRenderer>().material.SetColor("_Color", Color.red);
        }
    }
    
    [ServerCallback]
    void OnTriggerExit(Collider collision)
    {
        if (collision.gameObject.tag == "Block")
        {
            isSpawnBlocked = false;
            GetComponent<MeshRenderer>().material.SetColor("_Color", Color.yellow);
        }
    }
}