Advertisement
Pro_Unit

BoxSpawner

Jan 19th, 2022
1,211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.81 KB | None | 0 0
  1. using System.Collections;
  2.  
  3. using UnityEngine;
  4.  
  5. using Random = UnityEngine.Random;
  6.  
  7. public class BoxSpawner : MonoBehaviour
  8. {
  9.     public Box BoxPrefab;
  10.     public GameObject OuchModal;
  11.  
  12.     [SerializeField]
  13.     private float _timeStep;
  14.  
  15.     private void Start() =>
  16.         StartCoroutine(SpawnObjects());
  17.  
  18.     private IEnumerator SpawnObjects()
  19.     {
  20.         while (true)
  21.         {
  22.             yield return new WaitForSeconds(_timeStep);
  23.  
  24.             SpawnBox();
  25.         }
  26.     }
  27.  
  28.     private void SpawnBox()
  29.     {
  30.         float randomX = Random.Range(-2.21f, 2.21f);
  31.  
  32.         var spawnPosition = new Vector2(randomX, transform.position.y);
  33.  
  34.         Box box = Instantiate(BoxPrefab, spawnPosition, Quaternion.identity);
  35.        
  36.         box.Initialize(this);
  37.     }
  38.  
  39.     public void OnClick() =>
  40.         OuchModal.SetActive(true);
  41.  
  42.     public void OuchModal_Close()
  43.     {
  44.         OuchModal.SetActive(false);
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement