- Home /
Question by
variousdan · Feb 04, 2015 at 03:13 PM ·
2dshaderspritematerialmaterial renderer
Sprite Renderer Missing Material after running
I have a script that randomly changes the colour of several sprites and it works but the problem I am having is after exit on the first play test the material on the sprite renderers which was set to Sprites-Default becomes Missing (Material), during further play tests is type mismatch and the prefabs they were taking from become pink squares.
Heres the colour change code that I'm using.
using UnityEngine;
using System.Collections;
public class ChangeColour : MonoBehaviour {
public SpriteRenderer LeftArrow;
public SpriteRenderer UpArrow;
public SpriteRenderer RightArrow;
public SpriteRenderer Platform;
public SpriteRenderer Player;
public static float r;
public static float g;
public static float b;
float timeSinceLastCC;
void Start ()
{
r = Random.Range(0.1f, 1.0f);
g = Random.Range(0.1f, 1.0f);
b = Random.Range(0.1f, 1.0f);
}
void Update () {
timeSinceLastCC += Time.deltaTime;
if (timeSinceLastCC >= 10)
{
r = Random.Range(0.1f, 1.0f);
g = Random.Range(0.1f, 1.0f);
b = Random.Range(0.1f, 1.0f);
timeSinceLastCC = 0;
}
Platform.material.color = new Color(r,g,b);
LeftArrow.material.color = new Color (r, g, b);
UpArrow.material.color = new Color (r, g, b);
RightArrow.material.color = new Color (r, g, b);
}
}
Comment
Best Answer
Answer by smoggach · Feb 04, 2015 at 03:21 PM
If you modify the sprite-default material Unity will create an instance of it just for that sprite. Somehow you've managed to lose that instance.
With sprites though you're usually better off changing Sprite.color instead of Sprite.material.color.