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 /
This question was closed Jun 28, 2017 at 10:17 AM by HerryChoding for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by HerryChoding · Jun 28, 2017 at 07:17 AM · androidcolliderobjectcharacter

Need Help! My Score Text won't updated until the last object collected

Hello Everyone, I got a problem which I tried to look for the solution but still can't get it... My game already finished but the score text won't updated until the last object (in my case it was a ring which ad +5 score) collected and the score would show the actual score... And the problem only appear when i tried it on my phone... When i tried at Unity the score would updated as soon as the player collide into the object but not on my phone... Here my code on the coin object.

 public class CoinPickUp : MonoBehaviour {
 
     public int score;
     public Text scoretext;
     public int setscore;
 
     // Use this for initialization
     void Start () 
     {
     }
     
     // Update is called once per frame
     public void Update () 
     {
         scoretext.text = ("Score: " + score);
         setscore = score;
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Player") {
             score += 5;
             Destroy (gameObject);
         }
     }
 }

and the i put the collider on my player for the ring

 void OnTriggerEnter2D(Collider2D col)
     {
         if(col.tag == "FallDetector")
         {
             transform.position = respawnPoint;
         }
         if(col.tag == "Checkpoint")
         {
             respawnPoint = col.transform.position;
         }
         if(col.tag == "Ring")
         {
             GetComponent<AudioSource> ().Play ();
             cp.score += 5;
             Destroy (col.gameObject);
         }
     }






Comment
Add comment
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

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by Trevdevs · Jun 28, 2017 at 08:14 AM

Judging by the line where you destroy the gameObject im assuming your placing this script on the pickup object itself which you really shouldn't do as once the object is destroyed that instance of the code is destroyed with it thus deleting the score. Not sure as to why it doesn't work on your phone may just be a phyx bug.

But some thing I would recommend you try and who knows may solve the issue entirely

  1. Place one instance of the script on the player

  2. Write this ;p normally i don't do this but there was quite a few things I would change lol

      public class ObjectPickup : MonoBehaviour
         {
             public Text scoreText;
             private int score;
         
             void OnTriggerEnter2D(Collider2D col)
             {
                 switch(col.tag)
                 {
                     case "FallDetector":
                         transform.position = respawnPoint;
                         break;
         
                     case "CheckPoint":
                         respawnPoint = col.transform.position;
                         break;
         
                     case "Ring":
                         GetComponent<AudioSource> ().Play ();
                         score += 5;
                         scoreText.text = "Score: " + score;
                         Destroy (col.gameObject);
                         break;
         
                     default: 
                         Debug.LogError ("Collided Object tag not set in switch statement"); //May want to remove this if it stops your game everytime 
                         break;
                 }
             }
         }
    
    
    
    
Comment
Add comment · Show 1 · 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 HerryChoding · Jun 28, 2017 at 10:17 AM 0
Share

Thanks friends, it worked well~ Guess the scripts attached to the ring make the trouble for the score to update and some misplaced code as well... Thanks once again ^^

avatar image
0

Answer by Woltus · Jun 28, 2017 at 07:59 AM

Why do You update score text and score in different places? And why do You destroy coin two times?

To resolve your problem change coin script to something like that:

 void UpdateScore() 
 {
      score += 5;
      scoretext.text = ("Score: " + score);
  }
  
  void OnTriggerEnter2D(Collider2D other)
  {
      if (other.tag == "Player") {
          UpdateScore();
          Destroy (gameObject);
      }
  }
Comment
Add comment · Show 1 · 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 HerryChoding · Jun 28, 2017 at 10:15 AM 0
Share

I'm still beginner at program$$anonymous$$g and stuff so i followed few of tutorial and ended up in those scripts of $$anonymous$$e T T

And I have tried your code but it ended up make my score text appear only when i collider with the first coin and the score only stays at 5 no matter how many coin I collect... Don't know if i'm wrong or what...

Follow this Question

Answers Answers and Comments

156 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 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

invoke a prefab as child 1 Answer

How to grab objects in 3rd person LITTLE BIG PLANET STYLE 2 Answers

Raycast/Non-Physics Collider Discrepancy 0 Answers

Object detection in front of Character 1 Answer

object name disappearing!! 1 Answer


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