Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Pooling object and make it disappear as it collected

Discussion in 'Scripting' started by ddtna21, Sep 4, 2019.

  1. ddtna21

    ddtna21

    Joined:
    Aug 29, 2019
    Posts:
    3
  2. CurtisMcGill

    CurtisMcGill

    Joined:
    Aug 7, 2012
    Posts:
    67
    Hi ddtna

    I posted a reply that includes scripts and screenshots that answers your question.

    https://forum.unity.com/threads/nee...ger-in-subsequent-scenes.737309/#post-4918826

    You would create this script and attach it your coin

    Code (CSharp):
    1. using UnityEngine;
    2. public class Cash : MonoBehaviour
    3. {
    4.     // this is just an object to store money
    5.     public ProtoMoneyManager protoMoneyManager;
    6.     public float cashValue = 15;
    7.     public string player = "Player";
    8.     private void Awake()
    9.     {
    10.         protoMoneyManager = FindObjectOfType<ProtoMoneyManager>();
    11.     }
    12.     private void OnTriggerEnter(Collider other)
    13.     {
    14.         if (other.CompareTag(player))
    15.         {
    16.             protoMoneyManager.AddMoney(cashValue);
    17.             Destroy(this.gameObject);
    18.         }
    19.     }
    20. }
    In this script add the money to your ui.text or what ever.

    Code (CSharp):
    1. using UnityEngine;
    2. public class ProtoMoneyManager : MonoBehaviour
    3. {
    4.     public UI.Text txtMoney;
    5.     public float money;
    6.     void Start()
    7.     {
    8.         DontDestroyOnLoad(gameObject);
    9.         money = 100;
    10.     }
    11.     public float GetCurrentMoney()
    12.     {
    13.         return money;
    14.     }
    15.     public void AddMoney(float cashValue)
    16.     {
    17.         if (GetCurrentMoney() + cashValue <= 9999 - cashValue)
    18.             money += cashValue;
    19.  
    20.           txtMoney.text = money;
    21.     }
    22. }
     
    Last edited: Sep 4, 2019
  3. ddtna21

    ddtna21

    Joined:
    Aug 29, 2019
    Posts:
    3
    here are my codes. It only able to creat 5 coins, when i collected all of it the game stop.
    but if i raise coinsPoolSize, the game doenst work either.
    I want to spawn the coins when I play it and be able to collect. How can I make it works?




    Code (CSharp):
    1. public class Coin : MonoBehaviour
    2. {
    3.  
    4.     void OnTriggerEnter2D(Collider2D other)
    5.     {
    6.         if (other.GetComponent<Ufo>() != null)
    7.         {
    8.             GameControl.instance.UfoScored();
    9.             Destroy(gameObject);
    10.         }
    11.     }
    12.      
    13. }

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CoinPool : MonoBehaviour
    6. {
    7.     public GameObject CoinsPrefab;                                  
    8.     public int coinsPoolSize = 5;                                  
    9.     public float spawnRate = 2f;                                  
    10.     public float monstersMin = -1f;                                  
    11.     public float monstersMax = 3.5f;                                  
    12.  
    13.     private GameObject[] coins;                                  
    14.     private int currentCoins = 0;                                  
    15.  
    16.     private Vector2 objectPoolPosition = new Vector2(-10, -15);      
    17.     private float spawnXPosition = 4f;
    18.  
    19.     private float timeSinceLastSpawned;
    20.  
    21.    
    22.     void Start()
    23.     {
    24.         timeSinceLastSpawned = 0f;
    25.  
    26.        
    27.         coins = new GameObject[coinsPoolSize];
    28.      
    29.         for (int i = 0; i < coinsPoolSize; i++)
    30.         {
    31.            
    32.             coins[i] = (GameObject)Instantiate(CoinsPrefab, objectPoolPosition, Quaternion.identity);
    33.         }
    34.         InvokeRepeating("CoinPool", 2.0f, 0.3f);
    35.     }
    36.    
    37.  
    38.    
    39.     void Update()
    40.     {
    41.         timeSinceLastSpawned += Time.deltaTime;
    42.  
    43.         if (GameControl.instance.gameOver == false && timeSinceLastSpawned >= spawnRate)
    44.         {
    45.             timeSinceLastSpawned = 0f;
    46.  
    47.          
    48.             float spawnYPosition = Random.Range(coinsMin, coinsMax);
    49.  
    50.          
    51.             coins[currentMonsters].transform.position = new Vector2(spawnXPosition, spawnYPosition);
    52.  
    53.            
    54.             currentCoins++;
    55.  
    56.             if (currentCoins >= coinsPoolSize)
    57.             {
    58.                 coinMonsters = 0;
    59.                 Destroy(gameObject);
    60.             }
    61.         }
    62.     }
    63. }
    64.