Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want the key to appear after collecting all the coins

i am making a simple 2d platformer game and i really need some help. I have coins and the player needs to collect all of them. I also have a key and a door, door opens right after player gets the key. Here is the problem: Key is visible all the time but i want it to be visible once the player colllects all the coins so player can get the key and end the chapter. How can i make this happen?

This is my players code:
`

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Player : MonoBehaviour
{

    private Rigidbody2D myRigidbody;
    private Animator anim;
    private int score;
    public Text totalscore;
    [SerializeField]
    private GameObject playerhaskey, dooropened;
    [SerializeField]
    private int speed;
    private bool lookright;

    void Start()
    {
        myRigidbody = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        score = 0;
        lookright = true;
    }

    void Update()
    {
        float horizontal = Input.GetAxis("Horizontal");
        movements(horizontal);
        changedirection(horizontal);
    }


    private void movements(float horizontal)
    {
        anim.SetFloat("Walk", Mathf.Abs(horizontal));
        myRigidbody.velocity = new Vector2(horizontal * speed, myRigidbody.velocity.y);
    }

    private void changedirection(float horizontal)
    {
        if (horizontal > 0 && !lookright || horizontal < 0 && lookright)
        {
            lookright = !lookright;
            Vector3 direction = transform.localScale;
            direction.x *= -1;
            transform.localScale = direction;
        }
    }

    void OnTriggerEnter2D(Collider2D collider)
    {
        if (collider.gameObject.tag == "gold")
        {
            collider.gameObject.SetActive(false);
            score = score + 1;
            changescore(score);
        }
    }

    void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.tag == "key")
        {
            other.gameObject.SetActive(false);
            playerhaskey.SetActive(true);
            dooropened.SetActive(true);
        }
    }

    void changescore(int count)
    {
        totalscore.text = count.ToString();

    }
}

`

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is how the game screen looks like

>Solution :

First of all as a side note I would use the same technique for both key and coins. You could have a single OnTriggerEnter2D handle both.


Then I assume all coins are enabled at the beginning and apparently all have a tag gold anyway so you could simply do something like e.g.

private int totalCoinsRequired;

private GameObject key;

private void Start()
{
    // find the key object
    key = GameObject.FindWithTag("key");

    // find all coins -> amount needed to unlock key
    totalCoinsRequired = GameObject.FindGameObjectsWithTag("gold").Length;

    // initially hide the key
    key.SetActive(false);
}

...

void OnTriggerEnter2D(Collider2D collider)
{
    if (collider.CompareTag("gold"))
    {
        collider.gameObject.SetActive(false);
        score += + 1;
        changescore(score);

        // if score reaches required amount unlock key
        if(score >= totalCoinsRequired)
        {
            key.SetActive(true);
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading