I’m trying to spawn an object on network and then get reference for it by storing it inside an dictionary. Back a day i managed to do it with lightning object, but when i tried it again on my “mesh” object it seems like it can’t access the dictionary unless i initalize it inside the command.
Here’s a working code that i did for lightning a few months ago.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
//<PLR>
public class Lightning : NetworkBehaviour {
public GameObject lightningObj;
int key = 0;
IDictionary<int, GameObject> lightningDict;
public Color c1 = Color.blue;
public Color c2 = Color.white;
private void Start()
{
lightningDict = new Dictionary<int, GameObject>();
}
[Command]
void CmdSpawnLineRender(int key)
{
GameObject lightning = Instantiate(lightningObj, Vector3.zero, Quaternion.identity);
lightningDict.Add(key, lightning);
NetworkServer.Spawn(lightning);
}
public void DrawLightning(Vector3 startPoint, Vector3 endPoint, Vector3 hitTransPos, Vector3 direction, float length)
{
LineRenderer lineRenderer = lightningObj.GetComponent<LineRenderer>();
lineRenderer.widthMultiplier = 0.03f;
float alpha = 1.0f;
Gradient gradient = new Gradient();
gradient.SetKeys(
new GradientColorKey[] { new GradientColorKey(c1, 0.0f), new GradientColorKey(c2, 1.0f) },
new GradientAlphaKey[] { new GradientAlphaKey(alpha, 0.0f), new GradientAlphaKey(alpha, 1.0f) }
);
lineRenderer.colorGradient = gradient;
key++;
CmdSpawnLineRender(key);
}
}
Now i tried to do the replicate this trick on another gameObject but i keep getting following error
"NullReferenceException: Object reference not set to an instance of an object
FieldOfView.CmdSpawnAoe (Int32 key) (at Assets/Scripts/FieldOfView.cs:97)"
at Line 67. When i try to run the coresponding command on client connected to the host.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class FieldOfView : NetworkBehaviour
{
Vector2 PlayerPosition;
Vector2 CursorPosition;
Vector3 direction;
public float angle;
public float viewRadius;
[Range(0, 360)]
public float viewAngle;
public LayerMask targetMask;
public LayerMask obstacletMask;
public List<Transform> visibleTargets;
public float meshResolution;
public MeshFilter viewMeshFilter;
public GameObject vieshMeshFilterOnline;
IDictionary<int, GameObject> aoeDict;
int key = 1;
Mesh viewMesh;
Mesh viewMeshOnline;
public void Awake()
{
viewMeshFilter = Instantiate(viewMeshFilter, Vector3.zero, Quaternion.identity);
viewMeshFilter.transform.SetParent(transform.parent);
}
private void Start()
{
aoeDict = new Dictionary<int, GameObject>();
viewMesh = new Mesh();
viewMesh.name = "View Mesh";
viewMeshFilter.mesh = viewMesh;
DrawFileOfView();
CmdSpawnAoe(key);
}
private void LateUpdate()
{
DrawFileOfView();
}
[Command]
void CmdSpawnAoe(int key)
{
GameObject meshOnline = Instantiate(vieshMeshFilterOnline, Vector3.zero, Quaternion.identity);
[B]aoeDict.Add(key, meshOnline);[/B]
NetworkServer.Spawn(meshOnline);
}
I don’t get it why it doesn’t work now, it looks pretty much the same. In one stance it seems to see the initialization of dictionary outside of the cmd and it the other it doesn’t. I’m losing my mind here.
Both lightningObj and vieshMeshFilterOnline have networkID and are inside NetworkManager spawn list.