Question by
DrFury4D · Feb 21, 2021 at 12:13 AM ·
randomspriterenderercoin
Coin Flip not working
Hi!
I implemented a coin flip in my project.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinFlip : MonoBehaviour
{
SpriteRenderer spriteRenderer;
public Sprite[] sides;
public int flipCount = 0;
public float time = 0.005f;
public static int flips;
private void Start()
{
StartCoroutine(WaitPlease(time, 1.0f));
}
IEnumerator WaitPlease(float duration, float size)
{
for (int i = flipCount; i <= flips; i++)
{
while (size > 0.1)
{
size = size - 0.07f;
transform.localScale = new Vector3(1, size, 1);
yield return new WaitForSeconds(duration);
}
spriteRenderer.sprite = sides[flipCount % 2];
while (size < 0.99)
{
size = size + 0.07f;
transform.localScale = new Vector3(1, size, size);
yield return new WaitForSeconds(duration);
}
flipCount++;
}
}
private void Awake()
{
spriteRenderer = GetComponent<SpriteRenderer>();
flips = Random.Range(10, 12);
}
}
I created a scene to test it and it works just fine. As I hit play, the coin rotates and randomly it will show heads or tails.
The problem is when I call it in my main scene throught a button, it does not show me the other side of the coin (tails).
Element 0 is heads and Element 1 is tails. I don't understand why it doesn't work. I only have 2 lines for the button code:
GameObject headTail = Instantiate(coin, new Vector3(0, 0, 0), Quaternion.identity);
headTail.transform.SetParent(Arena.transform, false);
I really cant find my mistake.
Thank you!
unknown.png
(18.4 kB)
Comment