JSFiddle - React, Tailwind, and code Playground
HTML
RandomSpawn Spawn = new RandomSpawn();
private void OnTriggerEnter2D (Collider2D collision)
{
if (collision.tag.Equals("Coin"))
{
HealthBar.fill += 0.2f;
Destroy(collision.gameObject);
Spawn.SpawnRandom();
}
}
JavaScript
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomSpawn : MonoBehaviour
{
public GameObject[] listObjects;
public GameObject Coin;
int random;
void Start()
{
random = Random.Range(0, listObjects.Length);
SpawnRandom();
}
public void SpawnRandom()
{
Instantiate(Coin, listObjects[random].transform.position, Quaternion.identity);
}
}