pick up a item and count up

Hi, i have the following problem:

i wanna pick up a item called “butterfly” or “items” and i wanna count it up in a GUI Text. How can i make this?? I`m not a scripting pro. So maybe somebody can help me with a easy description!! Thanks!!

One way to do that is through collision or trigger events (all depends on you taste)… basically, it’s the same for both.
Your character must have a collider too, so when the collision it’s triggered, it will tell you… and there is where you can do the trick :smile:!

I’m at work now so i can’t tell you much (forget about an example xD!!)… but if nobody answer you, i will give you a more detailed example when arrive to home.

Well, here i am.

What i guess

  • You know the basics.
  • You character has attached a collider, script and a rigidbody.
  • You butterflies has a collider attached, with its property “isTrigger” checked.
  • You will do everything in this world to understand my english.

The main idea
When your character touches any butterfly, the butterfly will disappear and the butterflies counter will increase.

Character code
1- We have to define a variable to use it as a counter of catched butterflies.
2- We need to have a method called OnTriggerEnter(), which is executed by Unity on our character when a collision against a collider which has IsTrigger property enabled is done. Here is where the counter will increase.
3- Of course, OnGUI() methos is required to show the score :smile:!

Butterfly code
1- Basically, we only need OnTriggerEnter(); here we will put the code to kill the butterfly.

The magic

Here is the code of your character; omit the rest of the code you probably have with the sentences “some variables”, and “some methods”.

public class YourCharacterScript: MonoBehaviour
{
... some variables ...

	private int butterflyCounter;

... some methods ...

	private void OnTriggerEnter ()
	{
		//when the character collides agains butterflies, 
		//we increase the counter in one
		butterflyCounter += 1;		
	}


	private void OnGUI()
	{
		//Unity execute the method, and draws in every 
		//frame a label (transparent, of curse)
		//positioned in x=1 Y=1 with height=30 and width=200, 
		//and prints the value stored
		//in butterflyCounter);	
		GUI.Label(new Rect(1, 1, 200, 30),  "Butterflies: " + butterflyCounter);	
	}

}

The butterfly code is more easy, because we just only need to kill the gameObject to where the script is attached.

public class YourButterflyScript: MonoBehaviour
{
... some variables ...


... some methods ...

	private void OnTriggerEnter () {
			//Goodbye game object.
			Destroy(gameObject);
	}

}

As you can see, it’s very simple example.
If you don’t understand, i will try to explain better and more in deep; however, if you can’t understand my english… well, there is no help for that U_U

Thanks!!! So now iam at work :wink: i will test it when iam at home :slight_smile:

As i promised…
http://www.kokumo.com.ar/archivo/butterfly.rar

Very basic, but you will find it useful.

1 Like

Hi, I found this very useful… Thank you for taking the time to post this Kokumo :smile: