how to hange Sprites depening on a float value?
I have a gameobject that every 5 seconds spawns an Enemy prefab, and that enemy can have 5 different weapons, randomly chosen, and with each weapon, it has a different sprite, I got the weapons working, but the sprite wont change, can somebody help me?
private float Type; //Type is the random value
public Sprite Pistol; //Sprite with a Pistol
public Sprite Shotgun; //Sprite with a Shotgun
public Sprite SniperRifle; //Sprite with a Sniper Rifle
public Sprite Flamethrower; //Sprite with a Flamethrower
public Sprite Bazooka; //Sprite with a Bazooka
//*Al Sprites have been assigned in the editor
void Awake() //Its called before Start
{
Type = Random.Range(0, 10); //Type is assigned randomly between 0 and 9
Debug.Log("" + Type); //Just to see what was chosen
}
void Start()
{
if (Type < 3 && Type >= 0) //If value is 0 or greater and less than 3
{ // enemies with Type 0, 1 and 2 will have pistol
gameObject.GetComponent<SpriteRenderer>().sprite = Pistol;
}
if (Type < 5 && Type >= 3) //If value is 3 or greater and less than 5
{ //enemies with Type 3 and 4 will have Shotgun
gameObject.GetComponent<SpriteRenderer>().sprite = Shotgun;
}
if (Type == 5) //If Type is 5
{ //Only enemies with Type 5 will have a bazooka
gameObject.GetComponent<SpriteRenderer>().sprite = Bazooka;
}
if (Type < 8 && Type >= 6) //If Type is 6 or greater and less than 8
{ //Enemies with Type 6 and 7 will have a Sniper Rifle
gameObject.GetComponent<SpriteRenderer>().sprite = SniperRifle;
}
if (Type < 10 && Type >= 8) //If Type is 8 or greater and less than 10
{ //Enemies with Type 8 and 9 will have Flamethrower
gameObject.GetComponent<SpriteRenderer>().sprite = Flamethrower;
}
//As you can see, Pistol is the most common and Bazooka the rarest
}
I dont know why it wont work, I mean, It wont ever change from Pistol Sprite, which is already assigned to the prefab in the editor, and I dont want guys with a Pistol shooting rockets.
Thanx
In C# all variables should be written starting with small letter, not capital letter (i.e. pistol, not Pistol) . That usually makes problems. Capital letter is reserver for class/function names etc. Try changing that
Answer by Gunging · Sep 20, 2016 at 06:39 PM
I tried changing the capital letters to normal ones, still doesnt work :c