If you collect x amount of items you win the game

Hello, I’m new, so I don’t know much about scripting. Can someone help me with such a script?
Script should be working like this:
If “Player” collect all items by colliding/pressing Left Mouse Button then
shows up GUI Text: You have found all items! Retry or Quit? (for example)

Thanks alot! :slight_smile:

1 Like

Hey new guy, welcome to Unity! :smile:

Well thing you should do is write a simple script to find when you’re colliding with items then to increase your count

Try something like

//I defined this variable as an integer because we'll only need whole numbers for how many items have been collected

var count : int = 0;

//This function is called whenever an object's collider comes into contact with another objects. All of the collision information
//is stored in the variable collision (You can change the name) Like the other object and eveything (You can look look up the 
//API for more info

function OnCollisionEnter (collision : Collision)
{

//This if statement is using collision.transform (which is just referencing the other object in the collision's transform component
//It's also using it's tag, You can put tags on items so you can check them in scripting.
//For example if we didn't have that check, the counter would go up even if we hit a tree. The tag makes sure you're only
//Raising the count when you're colliding with an item

if(collision.transform.tag == "Item")
{

//Add one to count (many ways to do this, most common is count += 1, but it makes more sense when portrayed this way) 

count = count+1;

//Pretty straight forward, just destroys the object you can in contact with

Destroy(Collision.gameObject);

}

//Then you'll want a function that takes care of the game ending based on how many items you have
//Update is called every frame

var text : GuiText;

function Update ()
{

// >= means greater than or equal to I'm using 10 as an example for how many items you need, you might want to put a variable there

if(count >= 10)
{
//Here we create a new empty gameobject and store it in variable g, which is just used temporarily 

var g = new GameObject;

//Here we assign the text variable to a newly created GuiText component on the new game object. 

text = g.AddComponent(GuiText);

//Then just assign what you want the text to say

text.text = "Retry? | Quit?";
}
}

I hope this works and answers you’re question. I’m sorry if I over explained some thing, I’m assuming you’re fresh off the boat at programming, even though you’re probably smarter than that.

If you need any help or advice PLEASE don’t hesitate to PM me on the forums. I’d be happy to help :smile:

Won’t work, says that:

(35,14): BCE0044: expecting (, found ‘Update’.

? :stuck_out_tongue:

#pragma strict

var count : int;

private var text : GUIText;

function OnCollisionEnter(collision : Collision)
{
	if(collision.transform.tag == "dummy")
	{
		count += 1;
		Destroy(collision.gameObject);
	}
	print(Time.time);
}
function Update ()
{
if(count >= 10)
{
count = -1;

var g = new GameObject();

text= g.AddComponent(GUIText);

text.text = "Retry? | Quit?";

text.transform.position = new Vector3(0.5F,0.5F,0);

text.anchor = TextAnchor.MiddleCenter;
}
}

Ok this code should work, I didn’t put the comments back on so just look back at the other script, it’s the same except for the text options at the bottom. These just center the text on the screen.

Hope this helps, Good Luck with your game!

You’re script isn’t working for me…Do I have to tag the object I want to collect? What do I tag it?

You need to tag your object whatever the tag in the script is (in this case dummy) you can change this, but make sure your object is tagged correctly, tags are case-sensitive.

Interesting… I tried tagging it to dummy but when I clicked on it, it didn’t say it was collected even though it was destroyed (from a previous script I put on it.)

Clicked on it? The script makes you collect items when you collide with the object.

to click on it, you need to do a raycast. Check the unity scripting guide as they have an example, then just do the same things FlyingAce showed you.

Thanks! Sorry for the confusion.