Unity2d Instantiate & UI Count Problems.

spicisco

New member
Joined
Jul 15, 2022
Messages
2
Programming Experience
Beginner
SO I'm creating a little simple UNITY2D game where you drag and shoot a man into a Plinko type game area and have him collect pizzas. The game gives you a 3 heart system and respawns you back to starting position if you run into an enemy or fly off the map. I have a simple UI that displays the Pizza count in the corner. The issue comes with respawning which the player does when he collects a pizza, and runs into an enemy or falls off the maps.
So when my player respawns the previous pizzas collected count resets to 1 following his collection of a new pizza. So it never goes higher than one as the player respawns when a pizza is collected for another shot to collect more. Here's the individual scripts for my player and UI

What I need it to do is keep count of the pizzas collected even after respawning. The total number of pizzas collected should be 4 for each level and once all 4 are collected, the game goes to the next level. but it never counts more than one. Thanks!

Player Respawn:
public Transform respawnPoint;
    public GameObject playerPrefab;
    public GameObject deathEffect;

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Enemy"))
        {
            Instantiate(gameObject, respawnPoint.position, Quaternion.identity);
            Destroy(gameObject);
            Instantiate(deathEffect, transform.position, Quaternion.identity);
            AudioManager.instance.Play("Hurt");
        }
    }


Player Death:
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.transform.tag == "Enemy")
        {
            HealthManager.health--;
            if(HealthManager.health <=0)
            {
                PlayerManager.isGameOver = true;
                AudioManager.instance.Play("GameOver");
                gameObject.SetActive(false);
            }
        }
    }



Pizza UI:
public class PizzaUI : MonoBehaviour
{
    private TextMeshProUGUI pizzaText;

    // Start is called before the first frame update
    void Start()
    {
        pizzaText = GetComponent<TextMeshProUGUI>();
    }

    public void UpdatePizzaText(PizzaInventory pizzaInventory)
    {
        pizzaText.text = pizzaInventory.NumberOfPizzas.ToString();
    }
}




Pizza Inventory:
public class PizzaInventory : MonoBehaviour
{

    public Transform respawnPoint;


 public int NumberOfPizzas
    {
        get; private set;
    }

    public UnityEvent<PizzaInventory> OnPizzaCollected;

    public void PizzaCollected()
    {
        NumberOfPizzas++;
        AudioManager.instance.Play("Crunch");
        OnPizzaCollected.Invoke(this);
        Instantiate(gameObject, respawnPoint.position, Quaternion.identity);
        Destroy(gameObject, 0.1f);
    }
}




Pizzas Collected:
public class PizzaCollectible : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D collision)
    {
        PizzaInventory playerInventory = collision.GetComponent<PizzaInventory>();

        if (playerInventory != null)
        {
            playerInventory.PizzaCollected();
            gameObject.SetActive(false);
        }
    }
}



Player Drag n Shoot:
public class DragNShoot : MonoBehaviour
{
    public float power = 10f;
    public Rigidbody2D rb;

    public Vector2 minPower;
    public Vector2 maxPower;

    Trajectory tl;

    Camera cam;
    Vector2 force;
    Vector3 startPoint;
    Vector3 endPoint;


   private void Start()
    {
        cam = Camera.main;
        tl = GetComponent<Trajectory>();
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            startPoint = cam.ScreenToWorldPoint(Input.mousePosition);
            startPoint.z = 15;
        }

        if (Input.GetMouseButton(0))
        {
            Vector3 currentPoint = cam.ScreenToWorldPoint(Input.mousePosition);
            currentPoint.z = 15;
            tl.RenderLine(startPoint, currentPoint);
        }

        if (Input.GetMouseButtonUp(0))
        {
            endPoint = cam.ScreenToWorldPoint(Input.mousePosition);
            endPoint.z = 15;

            force = new Vector2(Mathf.Clamp(startPoint.x - endPoint.x, minPower.x, maxPower.x), Mathf.Clamp(startPoint.y - endPoint.y, minPower.y, maxPower.y));
            rb.AddForce(force * power, ForceMode2D.Impulse);
            tl.EndLine();
            AudioManager.instance.Play("ShootNorman");
        }
    }
}
 
It's not obvious to me how the chunks of code you have presented are connected to each other. From what I can tell through, you are already creating a new instance of PizzaInventory each time OnTriggerEntry2D() is called. Since each new instance doesn't know anything about previous instances, then each instance only ever bumps up its pizza count to 1.
 

Latest posts

Back
Top Bottom