Variable does not receive new value

Asked

Viewed 69 times

0

I have an object that should be "falling" over the screen several times, and it works.

What I’m trying to do is that a variable called x gets an extra 1 every time this object passes through the screen. When x receives 5 more, I want another variable called y to receive 1 less.

For this I declared both variables and assigned a value to them: x = 0 and y = 5.

PROBLEM:

x should receive 1 more when the object passes through the screen, and this happens, but only once. When the object first passes through the screen, x receives 1 more, but after that, x stops at 1 and receives nothing else.

inserir a descrição da imagem aqui

To y receive less 1, x must receive 5, so I believe that this is the reason why y does not change the value, since x is not more than 1.

No error message is displayed while running the game.

Why the value of variable x does not change?

CODE:

private int x, y;

void Start() {
    x = 0;
    y = 5;
}

void OnBecameInvisible() {
    // quando o objeto passar pela tela
    x += 1;
    Debug.Log("Valor de x: " + x);

    if ((x % 5) == 0)
    {
        // sempre que x receber mais 5
        y -= 1;
        Debug.Log("Valor de y: " + y);
    }
}

Observing: I didn’t put all the code because the rest of it is about the speed that the object will drop, position and things like.

  • We need to understand what is happening. For me it’s all right there, I see no problems. You have to show the problem happening. Read how to make a [mcve]

  • As I said in the question, the problem is that the value of both variables does not change. In Onbecameinvisible(), after x receives 1 more the variable has its value printed, and as you can see in the code, the same thing happens with the y variable. No error message is displayed while running the game, so it is difficult to know why!

  • I’ll edit the question and try to make it clear.

1 answer

0

Try changing the name of the object you are passing over the screen every time you activate the Onbecame function. It may be a possible solution. You can do the test by playing two objects on the screen and observing if the variable X receives a sum.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.