Author Topic: Score to string in NGUI.  (Read 11608 times)

faceOFF

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Score to string in NGUI.
« on: July 10, 2012, 01:57:08 AM »
Greetings to all!

I use JS:
  1. function Update()
  2. {
  3.         //draw the score in NGUI
  4.         GameObject.Find("LabelScore").GetComponent(UILabel).text += scorePoints;
  5. }
  6.  

Thus UILabel out display not the Score but infinite line of figures.
Example, if the Score is equal to zero, it deduces:
000000000000000000000000000000000000000000000000000 (infinitely).
How to solve? There is a something like .ToString () for (UILabel).text?

Excuse for my english ;)

dlewis

  • Guest
Re: Score to string in NGUI.
« Reply #1 on: July 10, 2012, 02:03:32 AM »
You should be keeping a local variable of the score and every time that is updated then update the label, don't use the use the label as data storage.

  1. int score = 0;
  2. score += scorePoints;
  3. label.text = score.ToString();

I use C# so I don't know if javascript has ToString so you could use...whatever the javascript equivalent is for printing a value to string and then assigning it.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Score to string in NGUI.
« Reply #2 on: July 10, 2012, 02:04:43 AM »
GameObject.Find in Update is a VERY slow operation.

Change it to a public reference instead:
  1. public UILabel scoreLabel;
...then set it in inspector via drag & drop.

P.S. This is C# code. I don't use javascript, so I can't support it.

faceOFF

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: Score to string in NGUI.
« Reply #3 on: July 10, 2012, 02:19:35 AM »
You should be keeping a local variable of the score and every time that is updated then update the label, don't use the use the label as data storage.

Wow! Great!!! Thanks a lot!
It's work in JS:
  1. function Update()
  2. {
  3.         //draw the score in NGUI
  4.         var score = scorePoints;
  5.         GameObject.Find("LabelScore").GetComponent(UILabel).text = score.ToString();
  6. }

GameObject.Find in Update is a VERY slow operation.

Ok, I will try, and to you thanks for council!