i want to generate random coin at runtime
i did some work but coin is generating at only place and limited can anyone tell m whats the mistake in this using UnityEngine; using System.Collections;
public class Random_coin : MonoBehaviour { public GameObject ObjectToSpawn; public int RandomRangeMax=30; public int ItemsToSpawn=20; public float ObjectLifeTime=120; public Vector3 Offset=Vector3.zero; //public Vector3 OffsetRandom=Vector3.zero; public float SpawnEverySecs=2.0f; public float WaitBeforeStartSpawning = 2.0f; public bool RandomOn=false; public int MaxInScene = 100; public string STRTag = "Enemy_ground";
GameObject GOShot;
int SpawnedItems=0;
float TimeFromLastSpawn=2000.0f;
bool Started = false;
bool Spawn = false;
// GameObject[] GOs;
// Use this for initialization
void Start ()
{
InvokeRepeating("RandomSpawn", WaitBeforeStartSpawning, SpawnEverySecs);
}
// Update is called once per frame
void RandomSpawn()
{
if (SpawnedItems < ItemsToSpawn)
{
Spawn = false;
if (!RandomOn)
Spawn = true;
else
if (Random.Range(0, RandomRangeMax) == 1) Spawn = true;
if (Spawn)
{
GameObject[] GOs = GameObject.FindGameObjectsWithTag(STRTag);
//Debug.Log(this.gameObject.tag+" "+GOs.Length);
if (GOs.Length < MaxInScene)
{
SpawnedItems++;
if (ObjectToSpawn)
GOShot = Instantiate(ObjectToSpawn, this.transform.position + Offset, Quaternion.identity) as GameObject;
if (ObjectLifeTime > 0.0f)
if (GOShot)
Destroy(GOShot, ObjectLifeTime);
}
//else Debug.Log("Spawn skipped");
}
}
}
}
Your answer
Follow this Question
Related Questions
How to Make an Object Slide When Instantiated? 0 Answers
I want to create a parabola of collectibles 0 Answers
How to save money 1 Answer
Array index is out of range. 1 Answer