Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 · 2d gametriggerunity 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
1

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.

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

Unity crashes 0 Answers

Does anyone know how to fix this unity 2d movement isssue? X axis movement not working 0 Answers

2D Platformer Pick up and move object script help! 1 Answer

How do I add force to a RigidBody2D? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges