How can i get sizeof(typeof(GameObject)) ?

Hi folk,
I was trying to get the size of GameObject just to know, how much memory a new empty GameObject takes.

I have tried it by:

Debug.Log("Size: " + sizeof(typeof(GameObject));

and

Debug.Log("Size: " + sizeof(GameObject);

but both are giving compiler time error.

Is there any other way to find?

You can try serializing a GameObject using a MemoryStream and a BinaryFormatter, as described here on Stackoverflow.

Thanks for your answer. It won’t work here. Throwing runtime exception:

SerializationException: Type UnityEngine.GameObject is not marked as Serializable.

I think the Stack Overflow page pretty much describes the various answers to your problem. I would personally keep it simple - look at memory used by the process, create x GameObjects, look at memory again, divide value by x. Not that this will be a precise indication of the memory footprint…

Out of curiosity, could you describe why you are trying to calculate this value?

sizeof will always return the size of the pointer for reference types, so it wouldn’t be accurate in this case. You’d have to add up the sizeof() of all the fields contained in GameObject.