Skip to content

Instantiating material due to calling renderer.material during edit mode #290

@Olney1

Description

@Olney1

When running the MCPForUnity editor tooling, the following warning is logged by Unity:

Instantiating material due to calling renderer.material during edit mode. This will leak materials into the scene. You most likely want to use renderer.sharedMaterial instead. System.Reflection.PropertyInfo:GetValue (object) MCPForUnity.Editor.Helpers.GameObjectSerializer:GetComponentData (UnityEngine.Component,bool) (at ./Library/PackageCache/com.coplaydev.unity-mcp@c7cf9cd32067/Editor/Helpers/GameObjectSerializer.cs:359) MCPForUnity.Editor.Tools.ManageGameObject:GetComponentsFromTarget (string,string,bool) (at ./Library/PackageCache/com.coplaydev.unity-mcp@c7cf9cd32067/Editor/Tools/ManageGameObject.cs:971) MCPForUnity.Editor.Tools.ManageGameObject:HandleCommand (Newtonsoft.Json.Linq.JObject) (at ./Library/PackageCache/com.coplaydev.unity-mcp@c7cf9cd32067/Editor/Tools/ManageGameObject.cs:157) MCPForUnity.Editor.MCPForUnityBridge:ExecuteCommand (MCPForUnity.Editor.Models.Command) (at ./Library/PackageCache/com.coplaydev.unity-mcp@c7cf9cd32067/Editor/MCPForUnityBridge.cs:1053) MCPForUnity.Editor.MCPForUnityBridge:ProcessCommands () (at ./Library/PackageCache/com.coplaydev.unity-mcp@c7cf9cd32067/Editor/MCPForUnityBridge.cs:894) UnityEditor.EditorApplication:Internal_CallUpdateFunctions ()

Expected behaviour

Editor tooling should use renderer.sharedMaterial when inspecting or serialising component data during edit mode, to avoid instantiating materials and leaking them into the scene.

Actual behaviour

The code path in GameObjectSerializer.GetComponentData calls renderer.material in edit mode, which causes Unity to create unique material instances and logs warnings.

Steps to reproduce

  1. Open Unity editor with MCPForUnity installed
  2. Trigger any action that runs ManageGameObject.GetComponentsFromTarget in edit mode
  3. Observe console warnings about renderer.material instantiation

Suggested fix

Update editor-side reflection/serialization code to use renderer.sharedMaterial when accessing Material properties during edit mode.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is needed

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions