-1
\$\begingroup\$

I wrote some code to spawn coins every 20 seconds but it spawns just 1. I'd like to make spawn coins from 1 to 4 coins randomly. Here's what I have currently:

I found a solution for this script I made 4 prefabs with different coins 1,2,3 and 4 and this script work perfectly.

Thanks to all for answers.

using UnityEngine;
using System.Collections;

public class CoinSpaws : MonoBehaviour
{

    public GameObject[] coins;
    int coinNo;
    public float maxPos = 1.7f;
    public float delayTimer;
    float timer;

    // Use this for initialization
    void Start()
    {

        timer = delayTimer;

    }

    // Update is called once per frame
    void Update()
    {

        timer -= Time.deltaTime;
        if (timer <= 0)
        {

            Vector3 coinPos = new Vector3(Random.Range(-1.7f, 1.7f), transform.position.y, transform.position.z);
            coinNo = Random.Range(0, 4);
            Instantiate(coins[coinNo], coinPos, transform.rotation);
            timer = delayTimer;

        }


    }
}
\$\endgroup\$
4
  • \$\begingroup\$ And where do you try to make it spawn from 1 to 4? \$\endgroup\$
    – Vaillancourt
    Jun 30, 2017 at 12:52
  • \$\begingroup\$ You can try to add a counter variable below the instantiate line. Something like; Instantiate(coins, coinPos, transform.rotation); counter+=1; When counter reaches your limit, then you make timer=delayTimer and make counter = 0. With that you need to also change your if statement \$\endgroup\$
    – Thalthanas
    Jun 30, 2017 at 13:44
  • \$\begingroup\$ eg. to spawn 1 coin 2 coins 3 coins and 4 coins randomly to spawn 1 or 2 or 3 or 4 \$\endgroup\$
    – Driton
    Jun 30, 2017 at 13:46
  • \$\begingroup\$ Post the answer as an answer, don't put it in the question. \$\endgroup\$ Jul 1, 2017 at 10:51

2 Answers 2

3
\$\begingroup\$

You could use a for loop and a random integer between 1 and 4.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CoinSpaws : MonoBehaviour
{
    public GameObject coins;
    public float maxPos = 1.7f;
    public float delayTimer;
    float timer;

    void Start()
    {
        timer = delayTimer;
    }

    void Update()
    {
        timer -= Time.deltaTime;
        if (timer <= 0)
        {
            Random rnd = new Random();
            int nbCoins = rnd.Next(1, 4);
            for(var i=0 ; i<nbCoins ; i++){
                Vector3 coinPos = new Vector3(
                    Random.Range(-1.7f, 1.7f), 
                    transform.position.y, 
                    transform.position.z);
                Instantiate(coins, coinPos, transform.rotation);
            }
            timer = delayTimer;
        }
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ i got a error with this code int nbCoins = rnd.Next(1, 4); Next is a error \$\endgroup\$
    – Driton
    Jun 30, 2017 at 14:51
  • \$\begingroup\$ @Driton: Random in this context is actually System.Random not UnityEngine.Random. The answer should be updated to reflect that (due to the ambiguous reference: a fully qualified name is required). \$\endgroup\$ Jun 30, 2017 at 16:31
  • \$\begingroup\$ You can replace Random rnd = new Random(); int nbCoins = rnd.Next(1, 4) with int nbCoins = Random.Range(1, 5); (ie. skip constructing a System.Random instance only to use it once and throw it away, when Unity's random generator can handle this case just fine) \$\endgroup\$
    – DMGregory
    Jul 1, 2017 at 17:27
0
\$\begingroup\$
 public class Coins
 {
    public GameObject Coin;
    public float spawnWait,setWait;
 }

 public class NewBehaviourScript : MonoBehaviour {

Coins coin;
//The position of coin 
public Transform coinPosition;

private void Start ( )
{
    StartCoroutine(SpawnerCoins());
}

IEnumerator SpawnerCoins ( )
{
    while (true) {
        //minCount and mixCount is min and max Values Of Coins you want
        for (int i = 0; i < Random.Range(minCount,maxCount); i++) {
            Instantiate(coin.Coin,coinPosition.position,Quaternion.identity);
            //spawnWait is how many seconds between each spawning coin
            yield return new WaitForSeconds(coin.spawnWait); 
        }
        //setWait is how many seconds between each set of coins 
        yield return new WaitForSeconds(coin.setWait);
    }
}
\$\endgroup\$
6
  • \$\begingroup\$ This will make multiple calls to Random.Range(...) (which, like the other answer is System.Random not UnityEngine.Random) which isn't necessarily desired and make result in a statistical skew towards fewer drops (getting 4 is a 9.3% probability, not the exepcted 25%). \$\endgroup\$ Jun 30, 2017 at 16:35
  • \$\begingroup\$ You mean should i use System.Random instead of Random.Range ? \$\endgroup\$
    – S J
    Jun 30, 2017 at 17:47
  • \$\begingroup\$ No, System.Random.Range() because UnityEngine.Random does not have a member method named Range \$\endgroup\$ Jun 30, 2017 at 19:13
  • \$\begingroup\$ @Draco18s Why do you say UnityEngine.Random does not have a member method called Range ? \$\endgroup\$
    – DMGregory
    Jul 1, 2017 at 17:29
  • \$\begingroup\$ @DMGregory I looked at the page and tried to find it and failed, that's why. The issue of the random probability of 4 draws is still only 9% though. I'm so glad no one's replied to that yet. \$\endgroup\$ Jul 1, 2017 at 17:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .