I have a method which takes a type and converts it into a byte array. But for some reason the byte array result always ends up with 0 in each index.
I don't know why this is happening, i am passing my own struct which does have values set, i am checking for that, so i shouldn't be getting 0 for every index.
This is my method to convert to a byte array:
public static bool TryGetBytes<T>(T obj, ref byte[] arr) { int size = Marshal.SizeOf(obj); if (size > arr.Length) { Debug.Log("Size error!" + size + " : "+arr.Length); return false; } IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(obj, ptr, true); Marshal.Copy(ptr, arr, size, 0); Marshal.FreeHGlobal(ptr); return true; } Is there anything i might be doing wrong here? Do i need ref at all ? I tried without ref and no difference occurred. So i am a bit confused how to get this to work.