Pick up Counter System need help!!!

I am new to Unity 3d and I am currently doing a project on Game Development. I want the objects picked up to be stored in a counter and displayed but I do not know how to. I’ve come up with the ‘Pick Up script’ which I’ve attached to the FPS Camera. There are 4 objects in my scene: sword, bow, arrow and shield. I have yet to include the health pickup. Is there any help that I can get from? or any guides/tutorials as I really need help now. Thank you. Here’s my script:

var arrow : GameObject;
var shield : GameObject;
var bow : GameObject;
var sword : GameObject;
var pickedSound : AudioClip;

function Update (){
}

function OnControllerColliderHit (hit:ControllerColliderHit)
{
if (hit.gameObject== GameObject.Find(“shield”) )
{
Destroy(hit.gameObject);
Debug.Log(“Object Collected!”);
audio.PlayOneShot (pickedSound);
}
if (hit.gameObject== GameObject.Find(“bow”) )
{
Destroy(hit.gameObject);
Debug.Log(“Object Collected!”);
audio.PlayOneShot (pickedSound);
}
if (hit.gameObject== GameObject.Find(“arrow”) )
{
Destroy(hit.gameObject);
Debug.Log(“Object Collected!”);
audio.PlayOneShot (pickedSound);
}
if (hit.gameObject== GameObject.Find(“sword”) )
{
Destroy(hit.gameObject);
Debug.Log(“Object Collected!”);
audio.PlayOneShot (pickedSound);
}
}
@script RequireComponent (AudioSource)

Do you want one counter that keeps track of how many items have been collected in total? Or one counter for each type of item (arrow, shield, bow, etc.)?

The concepts will be the same either way, but the details will be a little different, depending.

either way would be fine but if it’s stored individually would be better :slight_smile:

You’ll need a variable for each item you’re interested in, initialized to zero. If I were doing this I’d probably use containers rather than handling each item separately, but the easiest approach (given your current code) would probably just be to have a separate variable for each count.

When an item of a specific type is picked up, increment the counter for that type. For display, you can use the GUI system; create a script with an OnGUI() function, attach it to a game object, and in the OnGUI() function, use the GUI control of your choice (e.g. a label) to display the item counts.

okay sure, I’ll try that out. Thank you:)

“You’ll need a variable for each item you’re interested in, initialized to zero. If I were doing this I’d probably use containers rather than handling each item separately, but the easiest approach (given your current code) would probably just be to have a separate variable for each count”

Where do I exactly attach this script to? Thank you.

Are you asking where the code for this should go? If so, just think through it logically. There are three things you need:

  1. A ‘number collected’ variable for each item type.

  2. To increment the appropriate counter when an item is collected.

  3. To display the item counts.

I’d start with step 2, which is to increment the appropriate counter when an item is collected. So, you need to figure out which bits of code in your current script file executes when an item of a specific type is collected. (Hint: it’s where you do the other ‘item was collected’ stuff, like destroying the object and playing a sound.)

Once you know where the incrementing of the variable needs to go, that will give you a hint as to where the variable needs to be declared (item 1, above). It will need to be declared so that it’s accessible to the code corresponding to item 2, above, but also persists outside of that function. (Hint: think member variable, or whatever you would call the equivalent in UnityScript. Note that you already have some other item-related variables declared in that same scope.)

Once you get items 1 and 2 down, add some debug output letting you know whenever an item count has been incremented, and what the new count is; that’ll let you know whether things are working correctly so far.

For item 3, you’ll need an OnGUI() function that has access to the item counts. There are various ways you could do this, but the easiest would probably be to make the OnGUI() function a member of the same script that stores the item counts.

Finally, you can use a GUI control such as GUI.Label() to display the item counts.

So, take it one step at a time, and if you run into problems, post back here (be sure to include any code you’ve written), and someone will be able to help.

I’ve edited the script, and used guiText instead but I am still facing some problems. I don’t exactly know how to add some debug output letting you know whenever an item count has been incremented, and what the new count is; that’ll let you know whether things are working correctly so far.

And for the gui part, test.guiText.text = “Now get to the other side of the bridge!”;
I want it to appear after the test.guiText.text = “MAGNIFICENT! You’ve got bow =”+bows;
i need some kind of a delay because the “MAGNIFICENT! You’ve got bow =”+bows; appears too soon.
Thank you.

var test: GameObject;

var arrows : int = 0;
var bows : int = 0;
var shields : int = 0;
var swords : int = 0;

var arrow : GameObject;
var shield : GameObject;
var bow : GameObject;
var sword : GameObject;
var pickedSound : AudioClip;
function Start(){

test = GameObject.Find(“intro”);
//test.guiText.text= “Find missing item”;
}
function Update (){
}

function OnControllerColliderHit (hit : ControllerColliderHit)
{

if (hit.gameObject== GameObject.Find(“shield”) )
{
Destroy(hit.gameObject);
Debug.Log(“Object Collected!”);
audio.PlayOneShot (pickedSound);
shields++;
test.guiText.text = “BRAVO! You’ve got shield =”+shields;
}

if (hit.gameObject== GameObject.Find(“bow”) )
{
Destroy(hit.gameObject);
Debug.Log(“Object Collected!”);
audio.PlayOneShot (pickedSound);
bows++;
test.guiText.text = “MAGNIFICENT! You’ve got bow =”+bows;
test.guiText.text = “Now get to the other side of the bridge!”;
}

if (hit.gameObject== GameObject.Find(“arrow”) )
{
Destroy(hit.gameObject);
Debug.Log(“Object Collected!”);
audio.PlayOneShot (pickedSound);
arrows++;
test.guiText.text = “GREAT! You’ve got arrow =” +arrows;
}

if (hit.gameObject== GameObject.Find(“sword”) )
{
Destroy(hit.gameObject);
Debug.Log(“Object Collected!”);
audio.PlayOneShot (pickedSound);
swords++;
test.guiText.text = “GREAT JOB! You’ve got sword =”+swords;
}

}

@script RequireComponent (AudioSource)
@script RequireComponent(GUIText)