Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Editor/EditorCore/pb_ObjectArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ public T[] GetArray<T>()

for (int i = 0; i < array.Length; i++)
{
#if UNITY_6000_6_OR_NEWER
if (array[i] is T)
{
arr[i] = (T)System.Convert.ChangeType(array[i], typeof(T));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high
Using System.Convert.ChangeType for reference types (like Unity's Material, Mesh, or GameObject) will throw an InvalidCastException unless the runtime type of the object exactly matches T. It does not support casting through inheritance (for example, if array[i] is a MeshRenderer and T is Renderer).

Since the if (array[i] is T) check on line 33 already guarantees that the object is compatible with T, a direct cast is safer, more performant, and correctly handles inheritance as well as unboxing for value types.

Suggested change
arr[i] = (T)System.Convert.ChangeType(array[i], typeof(T));
arr[i] = (T)array[i];

🤖 Helpful? 👍/👎

}
else
{
arr[i] = default(T);
}
#else
if (array[i] is ProceduralMaterial)
{
arr[i] = (T)System.Convert.ChangeType(array[i], typeof(ProceduralMaterial));
Expand All @@ -44,8 +54,8 @@ public T[] GetArray<T>()
arr[i] = default(T);
}
}
#endif
}

return (T[])arr;
}

Expand Down