I’m trying to code a pick up that decreases your count, but something isn’t working. Whenever I collide with it the score remains, but the next good pick up sets my score to -1. I’m trying to just decrease by 1 or 2. I’m using the player controller script base from the 2D UFO tutorial and adding onto it, so how can i make this work? I’m currently using and Else If statement and the CompareTag function just like a regular pick up.
we’re not going to be able to help much without seeing what you have… post the code you have and we’ll see what we can see
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("PickUp")) {
other.gameObject.SetActive(false);
count = count + 1;
SetCountText(); }
else if (other.gameObject.CompareTag("Rocket"))
{
other.gameObject.SetActive(false);
count = -2;
}
This is my code that I have for the pick ups.
line 22:
count += -2;
This will just decrease it by 2, rather than setting it to negative 2.
That’s what I want, to decrease it. But it doesn’t decrease when I pick up the item, it stays the same and then when I pick up the object that’s supposed to add 1, it sets it to negative 1.
Thats because you forgot:
SetCountText();
It is working though, as -2 + 1 = -1, so all you need to do is display it.
I’m saying that it does that no matter the score. I’ve had the score at 4, and when I pick up the bad object the score doesnt change, but the next time i pick up a good item, its at negative 1. and I try to use SetCountText but it gives me an error.
Could u post your new code? I think you misinterpreted what I said.
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("PickUp")) {
other.gameObject.SetActive(false);
count = count + 1;
SetCountText(); }
else if (other.gameObject.CompareTag("Rocket"))
{
other.gameObject.SetActive(false);
count = -2;
SetCountText();
}
…
I fixed the error but still when I collect the bad object, no matter my score, It sets it to negative 2 now.
Ive put a space between the (-) and the 2 and it still does it
Just do this, it will work.
Thanks.