script for when 5 objects enters a cube go to next level

The final script I need for my game is for when a certain number a cubes with a tag enters a cube, we go to the next level. For example I get 5 cubes in the big cube then advance to next level.

Thanks

So you want someone to write it for you? You couldn’t even be bothered to try yourself? Not even a single attempt? How about you pay me for my time and I will write that script for you.

Enough of these GIVE ME SCRIPT post already…

lol Korno, settle down and just ignore the posts if they bother you so much.

AFX Design, I won’t write the script for you… Korno is being grumpy but he’s almost right.

The way I would do it:

Give each cube a unique id.
Connect them to a mommy object.
When the cube enters the scoring area, have it call mommy’s gainAPoint function
If the cube then leaves the scoring area (no idea if that’s possible) call mommy’s loseAPoint function

gainAPoint:
have a list of cubes
check list for the cube’s unique id
if it doesn’t exist, add the cube
if it does exist… that could be an error?

loseAPoint:
look for cube in list
if it’s in the list, remove it

How do we know when we have 5 cubes?
Find out the size of the cube list, that’s your “score” if it has 5 or more cubes in it, done.

Hope it helps.

Amending your answer gameObject all already have a unique ID, can be accessed with gameObject.GetInstanceID();

Amending all answers, because the “ID” is overcomplicating the problem a bit. There are two possible interpretations of the situation:
The smaller cubes disappear/are destroyed when they enter the bigger cube. Points are incremented each time.
The smaller cubes exist inside of the bigger cube until five are present (still movable), at which point it triggers a NextLevel function.

In the former case, you can just increment a simple counter each time one with the correct tag enters, then destroy them. Each increment, the counter would check against someGoalNumber and, if reached, run a function. Lists and IDs and such are irrelevant, because the objects can’t “exit”- it’s the final destination.

In the latter case, the objects are going to exist for the life of the problem, not be destroyed, so there’s no point in separating out an “ID”- just keep the references to the objects themselves in a list and check enters/exits against that directly. When the count of the list reaches 5, run the function.

Thanks image28, I never had any reason to use that so I really didn’t think of it. Great tip!