Hi there, I have a problem that I have been struggling with for several days now. I have a spell script that needs to call objects based on a prefab, which is stored in a ScriptableObject. An object is currently created using the ObjectCreator class, which is attached to the player. The main problem is that the SpawnObject method is called not on the server, but on the client, which means that the “prefab” variable is also changed only on the client, which leads to the “null reference” error. I’m using Unity.Netcode
ObjectCreator class
using UnityEngine;
using Unity.Netcode;
public class ObjectCreator : NetworkBehaviour {
GameObject prefab;
public void SpawnObject(GameObject _prefab) {
prefab = _prefab;
SpawnObject_ServerRpc();
}
[ServerRpc]
public void SpawnObject_ServerRpc() {
GameObject go = Instantiate(prefab);
NetworkObject netObj = go.GetComponent<NetworkObject>();
netObj.Spawn();
}
}
piece of spell code
author.GetComponent<ObjectCreator>().SpawnObject(data.summonedObjectPrefab);