Editing prefab instance material

Hi,

I have a character prefab which has a script that changes vert.001 child game object material, this is the hierarchy:

This character prefab’s script has vert.001 as a serialized parameter:

I want to instantiate a few characters and then change each one’s vert.001 material based on the team their are on

simplified code:

 private void SetTeamColorInModel()
    {
        Renderer playerRenderer = model.GetComponent<Renderer>();
        Material customMaterial = playerRenderer.material;
        customMaterial.SetColor("_Color", Color.gray); 
    }

The problem is I got the following error:

Not allowed to access Renderer.material on prefab object. Use Renderer.sharedMaterial instead UnityEngine.Renderer:get_material () CharacterScript:SetTeamColorInModel () (at Assets/Scripts/Characters/CharacterScript.cs:110)

What is the right way of changing a child object material?

1 Like

Having the same issue here. It’s weird that there are no answers anywhere.

Hey everyone- Also having this problem!

In this thread, @hexagonius mentions that this issue is due to the reference referring to the prefab object in the filesystem, not any runtime instance of the prefab: Not allowed to access Renderer.material on prefab object?

But if that was true- why do internal references for other properties work as expected? for instance, if I spawn a prefab with a heirarchy of gameobjects nested inside, and I change the position or rotation of one of those sub-objects in code via a component elsewhere in the prefab- it works. the reference I assign in the inspector applies to the instantiated instance if it’s transform, but somehow applies to the base prefab file if it’s a renderer. Why does this happen, and what is the correct way to assign materials (or properties of materials) to a renderer from code within a prefab?

A material is an asset in your project’s files, not part of the prefab itself. Very different to referencing a transform component that’s part of the prefab.

If you had a prefab that referenced the transform component of another prefab, then after instancing that reference would still point to the same prefab transform.

And importantly, when accessing .material for the first time it returns an instanced copy of the asset itself. So the error is a protection against doing something that would just leak material objects. Ones that would disappear after you closed/opened Unity, causing a bunch of confusing issues.

If you want to modify the prefab, I suggest saving that for runtime, and be sure to Destroy the instanced material when no longer required to prevent memory leaks.

This is not correct. Any reference to a renderer component inside the prefab itself will also apply to the instantiated instance on an instantiated prefab.

What are you trying to do exactly? If you’re trying to access the renderer in another prefab, or in an fbx model asset as in OP, then that’s the issue. It does not make much sense to do that, you’ll probably want to intantiate that asset first.

A screenshot or explanation of what you want to do exactly would help.

Thank you guys for getting back to me so quickly. And sure, lemme step back and explain.

  • I have a prefab that consists of a hierarchy of several objects. Much like any prefab, it is made of multiple gameobjects.
  • in one of the gameobjects of the prefab, I have a component that references the other gameobjects of the prefab. So within the one prefab, there are internal references from one gameobject to another in the prefab’s hierarchy.
  • later, at runtime, I call into an instance of this prefab. I want the prefab instance to manage its own materials on the various renderers that make it up, changing them at runtime. It should use the references I set earlier to do this. But, when I call into it, all the internal references it has to its own gameobjects and renderers, are not to the instance’s gameobjects and renderers, but those of the base prefab as hexagonius originally described. So I can’t change them.
  • It’s similar to OP’s situation which is why I bumped. In the prefab editor, I assign inspector references between objects, and later, in the instance, they refer to the base prefab.

is there a good way for a prefab to manage its own state internally like this? I feel like I’m missing something obvious.

Are any of these game objects you’re referencing children of a model prefab (having an icon akin to a “carved” box like in OP)? Model assets work a bit differently in Unity, you may need to unpack them before being able to use the ojects inside them freely.

If you’re not using any models, then there must be something else wrong. What you want to do should work like you intend without issues.

Your references to components/game objects are potentially referencing other prefabs assets, or your code that you think is operating on the instance is operating on the prefab asset itself by mistake.

I eventually intend to use this system with model prefabs nested inside, yeah. But right now, the issue is happening with no model prefabs nested in my prefab.

Absolutely. Yeah this feels like something I have to have done once already, lemme look and see if there is some other confounding issue.

You should post a screenshot of the hierarchy and the inspector where you’re referencing these objects. Also post any relevant code.