-1

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.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • Well `GameObject` is a **class** reference type => not a value type (like `struct`, `int`, `bool` etc) ... anything unclear about that? – derHugo Jan 01 '23 at 13:50

1 Answers1

0

I had a similar error (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 'ForceNetworkSerializeByMemcpy') and i found this fourm on unity (https://forum.unity.com/threads/non-nullable-values-in-networkvariable.1212075/) which talks about how it need to be a non nullable variable and since you can set game objects to null you need to make it so you cant set the game object to null. then i found this confusing thread on GitHub where they talk about an interesting clash of features between transform and gameobjects which you can read cause i suck at explaing things(https://github.com/JetBrains/resharper-unity/issues/1503). so in short you need to make the gameobject non nullable which i have no idea how to do, and everything i said may have been wrong cause i have no idea what I'm talking about that's why i included sources so i hope this helps and tell me if you find a solution cause i didn't find one

  • Thank you for your response, i figured out that i needed to make the 3 gameobject variables as non-nullable, but just like you, i absolutely don't know how to do it and i don't find anything on the net – Billibambessou Dec 31 '22 at 17:50
  • `you cant set the game object to null.` sure you can `GameObject example = null;` ... It is not a **Value** type though.. it is a **class** => reference type ... It makes no sense to synchronize class references as on the remote side the references are stored completely different... a reference is basically just a pointer to some memory locations.. => you can only synchronize things that can be completely reproduced on receiver side such as value types => for classes you would require an ID (-> Network identity) and go through a dictionary e.g. – derHugo Jan 01 '23 at 13:53
  • I said "...you need to make it so you cant set the game object to null," I know you can set a game object to null. – GamePro _awsome Jan 05 '23 at 01:13