• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by _Ady_ · Jul 04, 2017 at 03:06 PM · trigger2d gameunity 2d2d-platformer2d-physics

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 :)

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Andrea_Marchetti · Jul 04, 2017 at 03:18 PM 0
Share

Is this only place where you change the 'collectedCoins' value?

avatar image Drakonno · Jul 04, 2017 at 03:33 PM 0
Share

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.

avatar image tanoshimi · Jul 04, 2017 at 03:40 PM 0
Share

What is collectedCoins - a static variable? A member variable of this coin class, or of the player? We need more info.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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");

     }
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

88 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Way to achieve 3 lane mechanism in a 2D game 0 Answers

Trigger2D not working 0 Answers

Play and Stop Animation 1 Answer

HOLD JUMP BUTTON TO JUMP HIGHER 2 Answers

Problem with 2D movement. My character is moving by himself between two points... :) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges