dimanche 16 juillet 2017

Random sprite in Unity

void Start() {
    rch = Random.Range(0, 20);
    ball = GameObject.FindGameObjectWithTag("Ball");
    if (rch % 2 == 0) {
        ball.GetComponent<SpriteRenderer>().sprite = s1;
    }
    else
    {
        ball.GetComponent<SpriteRenderer>().sprite = s2;
    }

    Debug.Log(rch);
}

I have the following code which is supposed to select a number between 0 and 20 and if the number is divisible with 2 spawn one spite and otherwise the other.

The code is called every time the object is destroyed and that's why I use the start function. With Debug.Log(rch), I print the random number and even if the number is not divisible by 2, I only get the ball1.




Aucun commentaire:

Enregistrer un commentaire