• 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 Aqsa31 · Jan 19, 2016 at 07:43 PM · unity 5unity 2d

Problem in displaying the right text on screen

Hi I'm pretty new to this unity and I'm making a 2d game the problem I'm facing is displaying the right text in the screen , e.g I have a score count for keeping track of score and then I'm displaying this text through count.text , HOWEVER the problem is that when the game starts the text on screen displays "0", score is 0 as well, then I shoot an apple and the score becomes 1 as well as the count.text also becomes 1 however the text is still 0 on the screen, when I shoot another arrow the score and count.text shows value of 2 however on the screen it shows 1 and so on. I followed unity tutorial of roll the ball. here is my code

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class arrowcounttutorial : MonoBehaviour {
 
     public GameObject Arrow;
     public GameObject apple;
     public int score = 0;
     public Text count;
     
     // Use this for initialization
     void Start () {
         this.gameObject.GetComponent<Rigidbody2D> ().AddForce (transform.right*1500.0f);
         //score = 0;
         //showcounttext ();
         count.text =  score.ToString ();
     }
     
     
     // Update is called once per frame
     void Update () {
         Vector3 diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
         diff.Normalize();
         
         float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 0);
         
         if (Input.GetMouseButtonUp (0)) {
             
             
             GameObject bullet_new;
             
             bullet_new = Instantiate (Arrow,new Vector2 (-0.23f, -3.78f), Quaternion.identity) as GameObject;
             RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition),Vector2.zero);
             if (hit.collider!= null ) {
                 
                 LeanTween.move(bullet_new,  hit.collider.transform.localPosition, 1);
                 if(hit.collider.tag == "fruit")
                 {           
                     score++;
                     //showcounttext();
                     count.text =  score.ToString ();
                     print(count.text);
                     Destroy(hit.collider.gameObject,1);
                     Destroy(bullet_new,1);
 
 
                 }
                 
             }
             
             
         }
     }
 /*
     void showcounttext(){
 
         count.text =  score.ToString ();
     }
     */
 }

Also if I initialize the score in void start the text remains 0 through out the level even if the score and count.text are 1,2,3,4,5 so on. What can I do?

Comment
Add comment · Show 1
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 SterlingSoftworks · Jan 19, 2016 at 08:18 PM 0
Share

Question.. Why are you doing "ToString();"s after the scores..?

You could just move the count.text = score; into Update before all of the raycasting/collider checking. That way it should constantly update.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TheFish657 · Jan 19, 2016 at 09:24 PM

Use Update instead of Start

using UnityEngine; using System.Collections; using UnityEngine.UI;

  1. public GameObject Arrow; public GameObject apple; public int score = 0;

  2.   public Text count;
      
      // Use this for initialization
      void Start () {
          this.gameObject.GetComponent<Rigidbody2D> ().AddForce (transform.right*1500.0f);
    
  3.       //score = 0;
          //showcounttext ();
          
      }
      
    
  4. // Update is called once per frame void Update () {

      count.text =  score.ToString ();
    
          Vector3 diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
          diff.Normalize();
    
  5.    float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
          transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 0);
          
          if (Input.GetMouseButtonUp (0)) {
    
  6.        GameObject bullet_new;
              
              bullet_new = Instantiate (Arrow,new Vector2 (-0.23f, -3.78f), Quaternion.identity) as GameObject;
    
  7.           RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition),Vector2.zero);
              if (hit.collider!= null ) {
                  
                  LeanTween.move(bullet_new,  hit.collider.transform.localPosition, 1);
                  if(hit.collider.tag == "fruit")
    
  8.               {           
                      score++;
                      //showcounttext();
                      count.text =  score.ToString ();
                      print(count.text);
    
  9.                   Destroy(hit.collider.gameObject,1);
                      Destroy(bullet_new,1);
     
     
                  }
    
  10.        }
              
              
          }
    
  11.   }
      
    

}

Comment
Add comment · Show 7 · 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 TheFish657 · Jan 19, 2016 at 07:51 PM 0
Share

Sorry about the badly formatted code :P

avatar image Bonfire-Boy · Jan 19, 2016 at 09:53 PM 0
Share

But the OP is using Update.

avatar image SterlingSoftworks Bonfire-Boy · Jan 19, 2016 at 09:58 PM 0
Share

Yes, he IS using Update. HOWEVER in the original code the "text" is only changed one time. When he puts it outside of the raycast/collider checking, that makes it so the text is changed to the score every frame.

Thanks for doing exactly what I said in my comment Fish. ;P

avatar image Bonfire-Boy SterlingSoftworks · Jan 19, 2016 at 10:04 PM 0
Share

I think it would help if someone explained why this is expected to help. "Use Update ins$$anonymous$$d of Start" isn't actually what's going on here.

So can you either of you explain why this would make a difference? In the OP's code the text is being changed whenever the value of score changes. $$anonymous$$ight that not be considered better practice than setting it every frame?

I guess, perhaps, if the issue was that score was being changed elsewhere...

Show more comments

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Ball with bounciness 1 bounces higher and higher in unity2d,Ball jumps higher and higher when bounciness set to 1 1 Answer

Ads not working on the production build of android game 0 Answers

how do I find out which object is closer to the finish line? 2 Answers

How can i keep global scale of object while instantiate it 1 Answer

Custom Fill Origin 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