Counting the amount of prefabs on a platform.

Basically, I am making a puzzle game and I have these platforms that will go up or down only when a certain number of these prefab “Box” items I have are placed on the platform.

I want the platform to go down to the bottom of its path when the player gets 2+ boxes on this platform. If there are any less than 2 boxes on the platform, I want the platform to move up to the top of its path. Sort of like a platform that responds to the weight that is placed on it…

Basically I am having a hard time thinking up the logic that would detect the number of boxes on the platform being that they are essentially the same game object. How can I count the amount/number of these prefab boxes within a trigger area for example?

Don’t detect them. Count them when they…

I’m not following :eyes:

Something puts them on. Count them then.

I cant see a way in which that will work as an acceptable solution here

Well you don’t ellaborate other than a “physical” description of “on a platform” so we have no idea if your code puts them there on a platform (in which case, as mentioned aboved, you can simply count them then) or if they somehow fall on the platform(s) and contact via physics etc.

If it’s 2D physics then there’s the “GetContact” calls on Physics2D/Rigidbody2D/Collider2D which returns you all the contacts. You could retrieve the contacts for the platform, iterate the list looking for the items you’re interested in and count them, then take some action.

https://docs.unity3d.com/ScriptReference/Physics2D.GetContacts.html
https://docs.unity3d.com/ScriptReference/Rigidbody2D.GetContacts.html
https://docs.unity3d.com/ScriptReference/Collider2D.GetContacts.html

Here’s what I did for counting the prefabs. It was actually much simpler than I was thinking:

private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Object"))
        {
            count += 1;
        }
    }

private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Object"))
        {
            count -= 1;
        }
    }

This is how I have it working as of now: