Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Konkor4 · Oct 13, 2012 at 08:24 AM · guitextcounter

Delete gameObject and display how many deleted on-screen?

In my game you are required to find and collect objects, I've set it up so when you find one and click it they are deleted, how can I change a GUItext.text to count how many I've picked up? I've tried to do "GUItext.text += 1;" but if there is 5 objects it will say "11111" on the first delete. Could someone help to make it count them like 1, 2, 3, 4, 5?

 var PageCount : GUIText;
 var pickup : AudioClip;
 
 function Touched(hit:RaycastHit)
 {
 Destroy(gameObject);
 }
 
 function Update () {
     
 if ( Input.GetMouseButtonDown(0)){
        var hit : RaycastHit;
        var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        
     if (Physics.Raycast (ray, hit, 0.8))
       {
          hit.transform.SendMessage("Touched", hit, SendMessageOptions.DontRequireReceiver);
          PageCount.guiText.text = PageCount.guiText.text + 1; 
       }
    }
     
 }
Comment
Add comment · Show 2
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 Konkor4 · Oct 13, 2012 at 08:40 AM 0
Share

I see where you're co$$anonymous$$g from but I'm using JavaScript not C#...

avatar image whydoidoit · Oct 13, 2012 at 08:42 AM 0
Share

.ToString works in both... JavaScript isn't really JavaScript it's Unity Script and it has full access to all of the .NET framework.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by gribbly · Oct 13, 2012 at 08:40 AM

You're trying to increment a string. When used with strings, "+" means concatenate (join two strings). You need to increment a number. Try something like:

 var PageCount : GUIText;
 var count : int; //add this var
 var pickup : AudioClip;
 
 function Touched(hit:RaycastHit)
 {
     Destroy(gameObject);
 }
 
 function Update () {
 
 if ( Input.GetMouseButtonDown(0)){
     var hit : RaycastHit;
     var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 
     if (Physics.Raycast (ray, hit, 0.8))
       {
         hit.transform.SendMessage("Touched", hit, SendMessageOptions.DontRequireReceiver);
         count++; //increment count by 1
         PageCount.guiText.text = count; 
       }
    }
 }

...if you get an error, you may need to do something like:

 PageCount.guiText.text = count.ToString();
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 Konkor4 · Oct 13, 2012 at 08:46 AM 0
Share

Thanks, it works great. I actually had that before but I never used the .ToString(); so might be why it didn't work.

avatar image
1

Answer by T27M · Oct 13, 2012 at 08:36 AM

You are adding a int to a string which is going give you a string back, you need a int variable to hold the value of deleted items and add one to that then convert it ToString().

http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx

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

12 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

Related Questions

Problem with GuiFont - Not appearing 0 Answers

GUI Text Score counter 3 Answers

GUI text font change 3 Answers

Limit on GUI Components? 0 Answers

Using "fonts" that are actually images for a multi-outline effect. 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