I am trying to serialized some variables with INetworkSerializable in Unity 3D, so I am using a struct that contains the variables and the function to serialize them, here is my code:
public struct MyStruct : INetworkSerializable{
public static GameObject OBJ1 = GameObject.Find("Obj1");
public static GameObject OBJ2 = GameObject.Find("Obj2");
public static GameObject OBJ3 = GameObject.Find("Obj3");
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{serializer.SerializeValue(ref OBJ1);serializer.SerializeValue(ref OBJ2);serializer.SerializeValue(ref OBJ3);}}
then, in the start function, I delete every game object except the one that I want whose name is stored in the variable
string DoNotDelete = PlayerPrefs.GetString("ActiveObj");
MyStruct Struct = new MyStruct();
{if (Struct.OBJ1.name != DoNotDelete)
{DestroyServerRpc(Struct.OBJ1);}}
however, I am getting an error that says:
The type 'GameObject' must be a non-nullable value type, along with all fields at any level of nesting, in order to use it as parameter 'T' in the generic type or method '
BufferSerializer<T>.SerializeValue<T>(ref T, FastBufferWriter.ForPrimitives)
I get this error on the line 29 which is the line inside the NetworkSerialize<T>
function
I don't understand: variables on a struct aren't supposed to be non-nullable?
please note that I am still learning to code, and I do not perfectly understand how struct, non nullable value and things like that works
can someone explain me how to set those variables as non-nullable, and maybe if you are brave enough, how struct and non-nullable works? I have read the documentation but it makes no sense to me. thanks to the person that will answer.