- Home /
OnTriggerEnter2D(Collider2D other)
Hello everyone, I am working on a small 2d endless runner and I got a small problem. I have the following function
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.name == "Player")
{
collectedCoins++;
gameObject.SetActive(false);
coinSound.Play();
Debug.Log(collectedCoins);
}
}
My character runs on the platforms collects coins the script above deactivates the coins after the character is touching them but the collectedCoins value is not increasing as it should. Each platform haves 3 coins one after another, when the character touches the first coin debug.log shows 1 as I collected the first coin, once i touch the 2nd coin and 3rd coin the debug.log shows 1 again without increasing the total number. Now if on the next platform we have another set of 3 coins when i collect them debug.log will show 2,2,2 if i have a 3rd platform again with 3 coins and I collect them it goes to 3,3,3 but after this if the following 1-2 platform have no coins and later I get another platform with coins and i collect them my total number of coins from above (3,3,3) goes down to 2,2,2 or even 1,1,1 for no reason. What can i do? Is there a trick with the OnTriggerEnter2D function?
Thanks in advance :)
Is this only place where you change the 'collectedCoins' value?
First, if You don't do it, use some nice IDE, like VS. ;)
Second, as @Andrea_$$anonymous$$archetti asked, You probably change somewhere else that value. I think, that Your script detecting collision with coins is attached to something You spawn and despawn constantly (or even have multiple instances of it).
That "collectedCoins++" suggests You don't have reference to some "GlobalData" script holding well, data, and it is some private variable, dependent on lifecycle of platform/coin/player.
If You can, try changing question that it contains Your full script.
What is collectedCoins
- a static variable? A member variable of this coin class, or of the player? We need more info.
Answer by oranmooney · Jul 05, 2017 at 06:55 AM
Make sure you have the 2d collider on trigger, and have an audiosource and the audioclip to be the coin pick up sound. And if you are including a coin count UI then place it on the public Text in the hierarchy. Also this script was applied to the coin. Hope This Helps.
@Ady
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class PickUpCoin : MonoBehaviour {
public int collectedCoins = 0;
private AudioClip coinSound;
public GameObject theCoin;
public Text Scoretext;
void Start ()
{
collectedCoins = 0;
}
void Update ()
{
Scoretext.text = collectedCoins.ToString();
}
IEnumerator OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.name == "Player")
{
collectedCoins = collectedCoins + 1;
AudioSource audio = GetComponent<AudioSource>();
audio.Play();
yield return new WaitForSeconds(audio.clip.length);
audio.clip = coinSound;
audio.Play();
Debug.Log("Sound Was Played");
gameObject.SetActive(false);
Debug.Log("collectedCoins");
}
}
}
Answer by _Ady_ · Jul 04, 2017 at 03:57 PM
The script above was attached to the coins, now i made a new one that i attached to the player and it works.
void OnTriggerEnter2D(Collider2D col)
{
if(col.CompareTag("Coins"))
{
score++;
Debug.Log(score);
}
}
Still if anyone knows whats wrong with the first part that could help me learn new things. Thank you.