I am making a 2d map of stars. All the stars get random parameters like color. I want to change the star’s sprite according to it’s color variable. Right now this is my code (JS) in the star objects, i change the sprite in the start function:
function Start(){
starColor = GenStarColor();
starSpectrum = GenStarSpectrum();
starTemperature = GenStarTemperature();
starLuminosity = GenStarLuminosity();
Debug.Log(starColor);
Debug.Log(starSpectrum);
Debug.Log(starTemperature);
Debug.Log(starLuminosity);
var starSprite = Resources.Load("redStar");
GetComponent(SpriteRenderer).sprite = starSprite;
}
The game compiles, but i get the error: InvalidCastException: Cannot cast from source type to destination type. starControler.Start () (at Assets/Scripts/starControler.js:33)
Also no sprite is attached to the star’s spriterenderer.
I know that you could attach the sprite manualy in the inspector, but i am creating the star objects at runtime.
Anyone got an idea what i am doing wrong?