- Home /
Coin pickup script not working..?
Ello! Here i have a coin pinkup script that is not working. I am asking for help to give me a way on how i can fix this script. The Money script is assigned to my Player character and i have the Coin script on the coins game object (The coin script does nothing, it's simply used for the script as a sort of pickup system because it's easy that way.) The CoinCounter option on my Money script is using the UI Text from the Canvas. I would really appriciate any help i can get!
Money script (c#):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Money : MonoBehaviour {
private int moneyAmount;
[SerializeField]
private Text coinCounter;
// Use this for initialization
void Start () {
moneyAmount = 0;
}
// Update is called once per frame
void Update () {
coinCounter.text = "coins: " + moneyAmount.ToString();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.GetComponent<Money>() )
{
moneyAmount += 1;
Destroy(collision.gameObject);
}
}
}
And yes, i AM using the Coins collisions hitbox as Triggers.
Did you try resetting the physics settings? Does your player have a collider?
Answer by K00KIE · Jul 06, 2019 at 09:51 AM
if (collision.GetComponent<Money>() )
if this script is on the player should'nt you use GetComponent<CoinScript /*or wahtever is attached to the coin*/ >()
Because you are checking if the coin has money script.
As you said:
The Money script is assigned to my Player character and i have the Coin script on the coins game object
As the coin does not have money script it will not run.