- Home /
Changing material at runtime messes up with prefab materials [Solved]
Hi guys, I have a problem. I am making a Bubble Breaker game and so far I have made only the basic bubbles (red, green, blue, yellow, magenta). I have separate materials and prefabs for each bubble. Now I am trying to create a ChameleonBubble which changes it's color to one of the basic ones each turn.
Here is the code for adding the ChameleonBubble in the game scene:
public void AddChameleonBubbleAtRandomPosition()
{
int row = UnityEngine.Random.Range(0, Rows);
int column = UnityEngine.Random.Range(0, Columns);
Destroy(this.bubbles[row, column].GameObject);
this.bubbles[row, column] = null;
GameObject randomNormalBubble = this.GetRandomNormalBubblePrefab();
GameObject chameleonBubblePrefab = this.GetBubblePrefab(GameObjectName.ChameleonBubble);
GameObject newChameleonBubbleGameObject =
(GameObject) Instantiate(chameleonBubblePrefab, Vector3.zero, Quaternion.identity);
// Here I change the color of the chameleon bubble
// to a random one from the normalBubblePrefabs
newChameleonBubbleGameObject.renderer.material = randomNormalBubble.renderer.material;
newChameleonBubbleGameObject.transform.parent = this.transform;
newChameleonBubbleGameObject.transform.localPosition = new Vector3(column, -row, 0);
this.bubbles[row, column] =
new ChameleonBubble(row, column, this.GetTypeOfBubble(randomNormalBubble), newChameleonBubbleGameObject);
}
Everything works fine when I run the scene. The problem is that when I stop the scene, the materials of the prefabs of the normal bubbles are reset (In the MeshRenderer Component there is a message "Missing Material". That is only for the prefabs which materials have been used to create the new ChameleonBubble. For example (if in the scene a yellow ChameleonBubble appears, then only the material of the yellow normal bubble prefab is missing, the other normal bubble prefabs are ok). Do you know what the problem might be? I even tried "newChameleonBubbleGameObject.renderer.material = new Material(randomNormalBubble.renderer.material)" but that didn't work either.
Answer by Santa · Apr 15, 2014 at 05:18 PM
You are accessing randomNormalBubble.renderer.material. And randomNormalBubble is prefab.
Acessing 'material' creates new material automatically and replaces it in prefab. Renderer-material
You should use randomNormalBubble.renderer.sharedMaterial instead.
Thank you so much. It worked. I didn't know that. Thanks again. I don't have enough points to vote you up, but I will when I get those points, you can count on it :)
Thank You Santa!
This fixed an issue I only started having today, every other search finds this same issue with vague recommendations to use Inspector for prefabs (which I did use) and not use Resources.Load (which I did not use) and to copy prefabs by value rather than reference,
but only YOUR recommendation solved the issue caused by my usage of material rather than shared$$anonymous$$aterial, even though my usage was supposedly LHS read-only, that was enough to clear the assigned material from the prefab :)
I would also upvote this but I don't have 15 reputation yet...