[SOLVED]can't pick up my coins

hi guys I’m quite new in using unity could you please help me? i cant pick up my coins and it is making me a little frustrated.

thank you in advance.

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

public class PlayerScore : MonoBehaviour {

    private float timeLeft = 150;
    public int Points = 0;
    private bool isfinishLevel = true;

    public Text time;
    public Text scorei;
  
   
    void Update ()
    {
        if (isfinishLevel == true)
        {
            timeLeft -= Time.deltaTime;
            time.text ="Time Left:    " + (int)timeLeft;
            scorei.text ="Score:    " +  Points;
            //Debug.Log(timeLeft);
            if (timeLeft < 0.1f)
            {
                SceneManager.LoadScene("Gameplay");
            }
        }
    }

    void OnTriggerEnter2D (Collider2D trig)
    {
        if (trig.tag == "End")
        {
            isfinishLevel = false;
            CountScore();
        }
        if (trig.tag == "score")
        {
            Points += 10;
            Destroy(trig.gameObject);
        }
    }

    void CountScore()
    {
        Points = Points + ((int)timeLeft * 10);
        //Debug.Log(Points);
    }

Did you set up your colliding objects correctly?

OnTriggerEnter2D will be called only if the object with that function has a Collider2D set to be a Trigger, the other object has a Collider2D, and at least one of the two objects has a Rigidbody2D.

Is your function being called? Use Debug.Log to find out where the code is failing.

1 Like

yes i have collider set to intrigger for my coin. the two objects has a Collider2D, and atleast one of them have Rigidbody2D.
last time i opened my project i can collect them then when i opened it awhile ago it stops working. i dont know what just happened. the inspector for my coin is this:

3169286--241371--coin.png

So is the OnTriggerEnter2D function getting hit?

Yes it is, and i don’t know what happened :frowning: it suddenly doesn’t work

So if the function is being hit, what isn’t working?

Is the code reaching inside the IF statement for the “score” tag?

Try printing out which collider that function is receiving, and what its tag is.

where would i place the Debug.Log() to print that?

You could try something like this:

void OnTriggerEnter2D(Collider2D trig) {
    Debug.Log(trig.gameObject.name + " detected. Has tag: " + trig.tag);
    if(trig.tag == "End") {
        isfinishLevel = false;
        CountScore();
    }
    if(trig.tag == "score") {
        Points += 10;
        Destroy(trig.gameObject);
        Debug.Log("10 points added, " + trig.gameObject.name + " destroyed");
    }
}

No message appears in the Console. Maybe my colliders does not wok???

If you get no messages at all, then yes your collider isn’t detecting the collision, or you’ve got your script in the wrong place perhaps.

Double check that you have both collider objects on GameObject layers that can interact. Are they both on Default layer, or perhaps custom layers? Physics Layer interaction is defined in Edit → Project Settings → Physics2D.

Make sure that this script is on the object or a parent of the collider. Make sure that the player’s collider is indeed a Trigger.

I’d also recommend sticking to all-lowercase tags, since they’re case-sensitive. It rules out another class of potential problems :slight_smile:

i was just following a tutorial in youtube. i can’t really seam to find what is wrong.

My advise to you is to follow it again, and verify you did every step correctly.

There’s no need to be frustrated. Be patient with yourself and you will learn from this problem.

1 Like

Can you take a pic of your player gameobject in the inspector (like you did with your coin), and a pic of your physics2D settings edit → projects settings → physics2D

Okay thank you by the way :slight_smile:

here it is :slight_smile:

3169475--241381--Capture.PNG
3169475--241382--Capture1.PNG
3169475--241383--Capture2.PNG
3169475--241384--Capture3.PNG

Where is your PlayerScore component?

thank you for the tip :smile:

its in my time&Score Canvas. it worked just fine the other day then when i opened the project again. it is not working. T.T

The script that defines any physics callbacks like OnTriggerX and OnCollisionX, must be on the same object as the collider doing the colliding, or on a parent object.

I have a feeling the tutorial you followed may have done something differently than you have here. I actually have to go now but if this hasn’t been resolved by the time I’m back I’ll continue to help you.