Hey, i’m new to Unity and i’ve been trying to figure it out myself, i made it work by just adding colliders and using OnCollisionEnter but that would only work when dealing with one object.
So basically what i’m trying to do is whenever a block (lets say block lv.1) drops from the sky and touches the floor, it will add 1 cash, then when a block lv.2 drops this time it’ll add 2 cash, and so on. How do i do this, how do I make the floor look for the block name and lv so it can add the cash accordingly depending on the level of the block. Thanks.
There’s several ways you can approach this. For something simple where you’re just incrementing the dropped cash provided by 1 each time, you could just use a static int for the amount of cash to provide next, which you increase each time cash is provided. Oversimplified example below:
private static int nextCash = 1;
void OnCollisionEnter(Collision collision) //Block just hit the floor
{
provideCash(nextCash);
nextCash += 1; //increment the value of the next block by 1
}
private void provideCash(int cash)
{
//Whatever code adds cash to the player's total
}
For something more advanced you could instead create a cash manager script which your OnCollisionEnter calls. It could have a more complex system for incrementing the cash from the next block. Maybe you want to make is so blocks that hit within a certain time from the last one provide more cash, or maybe you want different types of blocks to increment independently of each other (blue blocks increment when only blue blocks hit, same with green blocks, but green blocks start at a much higher value than blue, etc, etc).
Good luck