- Home /
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;
}
}
}
I see where you're co$$anonymous$$g from but I'm using JavaScript not C#...
.ToString works in both... JavaScript isn't really JavaScript it's Unity Script and it has full access to all of the .NET framework.
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();
Thanks, it works great. I actually had that before but I never used the .ToString(); so might be why it didn't work.
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
Your answer
Follow this Question
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