Why do playmode copied gameobjects lose their material when pasted?

Hello everyone, I’ve been having an issue I didn’t expect to be coming across. What I’m trying to do is create a level editor of sorts, where I select a grouping of hexes in playmode and place them as children of a transform. Easy right? They are instances of prefabs after all.

Well the problem introduces itself once I copy that grouping, exit playmode, and paste them in the editor. All the materials go pink, because they’ve been removed for some reason. I’m not sure why I haven’t come across this issue as it may just be basic knowledge, but I’ve scoured the internet, and I haven’t really come across any similar problems as solutions are related to improper URP imports.

In playmode

Prefab

Exited playmode and pasted

I’m really unsure why the material is removed, they are just instantiated prefabs so I see no reason why their material would be disassociated. Any insight on this would be well appreciated.

it says (instance) in the material at playmode,
9546097--1348600--upload_2023-12-24_12-6-37.png

that means it has been modified/created/cloned or something else during playtime (so it wont be available after exit playmode)

Edit:
So I have recently learned, and tested, that objects lose their materials when meshrender.material is modified during playtime. This instance material is destroyed, because it’s been modified sort of independent of the gameobject, essentially destroying the material when I want to be copying it into the scene for afterwards, and causing it to turn pink. I would strongly recommend adding an editor helper script to a gameobject, or at least have a reference for it, and you could make it run in the editor when you exit playmode so that it can have a reference to the material. Such as this:

using UnityEngine;

[ExecuteInEditMode]
public class ExampleClass : MonoBehaviour
{
    [SerializeField] Material material;
    void Start()
    {
        if (!Application.IsPlaying(gameObject) && GetComponent<Renderer>().sharedMaterials[0] == null)
        {
            GetComponent<Renderer>().sharedMaterial = material;
        }
    }
}

If you for example, attach this to a gameobject, and then assign a material, the material will be associated as soon as the game object is pasted.

I would strongly recommend assigning the material before, or at runtime, before any modifications to the gameobject’s material is made

Assuming anyone else on the Internet runs into this predicament.

1 Like

Thank you for this information, this behavior was unforeseen for me. What I was doing was modifying the color to its default color, which would cause this issue even if visually it is identical.