top of page
Search

Coin Pickup

  • Writer: Keshav Batra
    Keshav Batra
  • Dec 16, 2022
  • 1 min read

I now have a coin pickup function where the number increases whenever I collect a coin. I did this by creating a script for my coin pickup and adding to my player movement. I created an OnTriggerEnter function in the pickup script where whenever the player collides with the coin and has the tag "Player" on it, the number increases using get component to connect to the player movement script and add the amount using CoinAmount++. The player script contains the coin amount int so that it gets displayed in the inspector of the pickup.



Coin Script:

{
    public GameObject Sphere;


    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player") //Senses the pickup whenever the player has the tag
        {
            other.GetComponent<PlayerMovement>().CoinAmount++; //Adds 1 to the amount of coins 
            Destroy(gameObject); //Destroys the pickup
        }
    }
}


Player Script:



 void CoinPickup()
    {
        GUI.Label(new Rect(10, 10, 100, 20), "Score : " + CoinAmount); //Displays the coin amount in the inspector
    }


 
 
 

Comments


bottom of page