Search Unity

Random Sprite Generation

Discussion in '2D' started by rhianleake96, Mar 16, 2018.

  1. rhianleake96

    rhianleake96

    Joined:
    Feb 26, 2018
    Posts:
    1
    I'm building a 2D game where you have to swipe upwards on a phone to grow a plant from where you touch. I have about 4 animations to include and I want them to generate randomly with each tap. I have them changing colour randomly but I have no idea how to get them rendering randomly!

    HELP!
     
  2. furroy

    furroy

    Joined:
    Feb 24, 2017
    Posts:
    93
    Add public List<Sprite> AllSprites to your class so you can populate it in the Editor then swap the sprites out at random


    public List<Sprite> AllSprites;

    // later on inside your logic
    SpriteRenderer sr = GetComponent<SpriteRenderer>();
    if (sr) sr.sprite = AllSprites[Random.Range(0, AllSprites.Count)];
     
    Long-as-Python likes this.