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

Question 2D how to spawn an object after meeting if condition

Discussion in '2D' started by killer2005121, May 8, 2023.

  1. killer2005121

    killer2005121

    Joined:
    Apr 21, 2023
    Posts:
    3
    So I'm making a little game for school as project and I'm having trouble with spawning an object on a set location after meeting my condition. Condition being after picking up 3 coins an object should spawn at the location that I have set it. But it doesn't spawn even if I have picked up the 3 coins (There arer only 3 coins on the map). Could someone tell me what is the wrong in my code? And in the image is the how I have set it up.


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class ItemCollecter : MonoBehaviour
    8. {
    9.     public int coins = 0;
    10.  
    11.     [SerializeField] private Text coinsText;
    12.     [SerializeField] private GameObject objectToSpawn;
    13.     [SerializeField] private GameObject spawnToObject;
    14.  
    15.     private void OnTriggerEnter2D(Collider2D collision)
    16.     {
    17.         if (collision.gameObject.CompareTag("Coin"))
    18.         {
    19.             Destroy(collision.gameObject);
    20.             coins++;
    21.             coinsText.text = "Coins: " + coins + "/3";
    22.  
    23.             if (coins == 3)
    24.             {
    25.                 Instantiate(objectToSpawn, spawnToObject.transform);
    26.             }
    27.         }
    28.     }
    29. }
    30.  
     

    Attached Files:

    Last edited: May 8, 2023
  2. gooby429

    gooby429

    Joined:
    Aug 13, 2019
    Posts:
    116
    youre destroying the object with the coin count on it so it never goes past one, you just coins++ then destroy it. you need to move the coins count to a different object like a manager, then increment that counter instead
     
  3. killer2005121

    killer2005121

    Joined:
    Apr 21, 2023
    Posts:
    3
    Could you show me please how it should look I'm pretty new so I would be really grateful if you could do that (if I'm not bothering)
     
  4. gooby429

    gooby429

    Joined:
    Aug 13, 2019
    Posts:
    116
    heres a simple way:

    Make a new script with the coins field in it like this, and place it on an object in the scene.
    Code (CSharp):
    1. public class GameManager : MonoBehaviour
    2. {
    3.     public int coins = 0;
    4. }
    5.  
    Modify your class to remove the coins and add a reference to the GameManager, and dont forget to assign it in the inspector after recompile
    Code (CSharp):
    1. public class ItemCollecter : MonoBehaviour
    2. {
    3.     [SerializeField] private Text coinsText;
    4.     [SerializeField] private GameObject objectToSpawn;
    5.     [SerializeField] private GameObject spawnToObject;    
    6. [SerializeField] private GameManager _gameManager;
    7.  
    8.     private void OnTriggerEnter2D(Collider2D collision)
    9.     {
    10.         if (collision.gameObject.CompareTag("Coin"))
    11.         {
    12.             Destroy(collision.gameObject);
    13.             _gameManager.coins++;
    14.             coinsText.text = "Coins: " + _gameManager.coins+ "/3";
    15.             if (_gameManager.coins== 3)
    16.             {
    17.                 Instantiate(objectToSpawn, spawnToObject.transform);
    18.             }
    19.         }
    20.     }
    21. }
    22.  
    ideally you should also move the spawning stuff into the game manager too but im confident you can figure that out :)
     
  5. killer2005121

    killer2005121

    Joined:
    Apr 21, 2023
    Posts:
    3
    Ok thank you very much for the information