- Home /
Coin Counter still not working after 5+ hours of frustrating changing code and moving stuff around
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public int currentCoins;
int maxCoins = 22;
void OnGUI()
{
GUI.Label(new Rect(10, 10, 150, 50), "Coins Collected: " + currentCoins + " / " + maxCoins);
}
private void OnCollisionEnter(Collision col)
{
currentCoins = currentCoins + 1;
}
}
Here is my code. I've linked it to every different part of my dude, and it never works. I've tried combining the scripts and linking scripts together, nothing has worked. I'm trying to make everytime my character hits a coin (I've changed it to every object because coin wasn't working) for the gui score counter to go up. What am I doing wrong? Very frustrated. Thanks. Literally all I want to do is detect when I collide with an object tagged coin, or even just check if I collide with anything at this point. I;ve made working teleporters that use the exact same code and principles so why doesn't this work in any form
couple things before i give an answer 1. is it a 2D game or 3D, ie, if you put a Collider2D on your prefab/asset/sprite/player but you're calling OnCollissionEnter, they won't communicate it's like spanish to chinese with not translator. It's also possible that, you have an agreeing collider and call method but you forgot to check the box "isTrigger" in the editor.
would you consider using UI ins$$anonymous$$d of GUI, i can prolly get this to work on a UI but I've always stayed away from GUI so i can't give you GUI script but i can make it work in a UI script
Do you have colliders both on your coins and your player and a RigidBody at least one of them?
Answer by MrRightclick · Apr 12, 2019 at 06:51 AM
Are you using 2D or 3D colliders? You're using the 3D version of the OnCollisionEnter method, so you must use a non-trigger 3D collider (a collider that doesn't have the 2D suffix in it's name). As you're using OnCollisionEnter, you must also have at least one non-kinematic rigidbody on either of the colliding objects (probably the player in your case).
You should first and foremost check your colliders and their settings. If any of them are trigger colliders, this method won't work, so you must either make them physical (non-trigger) colliders OR you should be using OnTriggerEnter instead.
It's also always good to read up on documentation in case you are having problems:
Answer by hameed-ullah-jan · Apr 12, 2019 at 08:41 AM
Make sure the following things are set up correctly:
The object to which this script is attached must have a collider (if using mesh collider Check the convex option if unchecked)
he object to which this script is attached must be having a rigid body, with "iskinematic" option un-checked
The collider's "IsTrigger" option must be un-checked
If all of the above settings are correct, then go to Layer Collision Matrix and check if there are layers which are set to not detect some other layers