Material only sets correctly once

I created a custom shader, and then a custom material that uses that shader called “Herber1”. I have a GameObject prefab that has a child object called “character” that contains a Sprite (with a SpriteRenderer). I’m trying to set the material on a cloned prefab.

The frustrating thing is that it works exactly once, on the first call. However, all subsequent calls for more clones do not change the material. I’ve tried the following, and they all work only once:

hero.mySprite.Find(“character”).GetComponent(SpriteRenderer).material = Resources.Load(“herber2”, Material);

hero.mySprite.Find(“character”).GetComponent(SpriteRenderer).sharedMaterial = Resources.Load(“herber2”, Material);

hero.mySprite.Find(“character”).renderer.material = Resources.Load(“herber2”, Material);

All three statements work the first time, but all subsequent calls leave the material unchanged (Sprite-Default).


Solved my own question, thought it might help others to post the solution. My problem was that my Find was executing off of GameObject rather than Transform. So, it was finding an object in the scene named “character” rather than a child of the parent gameobject.

hero.mySprite.Find(“character”).GetComponent(SpriteRenderer).material = Resources.Load(“herber2”, Material);

needed to be:

hero.mySprite.transform.Find(“character”).GetComponent(SpriteRenderer).material = Resources.Load(“herber2”, Material);


Material loadedMaterial = Resources.Load("herber2", Material);

hero.mySprite.Find("character").GetComponent(SpriteRenderer).material = loadedMaterial;
 hero.mySprite.Find("character").GetComponent(SpriteRenderer).sharedMaterial = loadedMaterial;

hero.mySprite.Find("character").renderer.material = loadedMaterial;

I didn’t proof it, but it seems the only odd thing is you’re loading the same asset 3 times.