my variable doesn't change even that in the log its say the variable change

I have 1 script to PlayerMovement and one for powerUp I the power-up code I reference player movement to change the speed and change the bool named timer to true and I write that in log and when I touch the paper the speed doesn’t change and the timer don’t turn to true but in the log, its say that is yes

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    private float TargetPos;
    public float Speed;


    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
       
        transform.position = new Vector2(TargetPos, transform.position.y);
    }

    public void right()
    {
        TargetPos = transform.position.x + Speed * Time.deltaTime;
    }
    public void left()
    {
        TargetPos = transform.position.x - Speed * Time.deltaTime;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Powrups : MonoBehaviour
{

    public PlayerMovement pm;
    public float PowerUpActiveTime;
    public float StartPowerUpActiveTime;
    public float peperSpeed;
    float NormalSpeed;
    bool timer;
    bool timerover;


    private void OnTriggerEnter2D(Collider2D col)
    {
        if (col.name == "peper")
        {
            pm.Speed = peperSpeed;
            timer = true;
            Debug.Log("timerOn");
            Debug.Log(pm.Speed);
            Debug.Log(timer);

        }
    }
    private void Update()
    {
        while(timer)
        {
            GameObject Pause = GameObject.Find("Pause");
            PauseScript pausescript = Pause.GetComponent<PauseScript>();
            if (!pausescript.pause)
            {
                PowerUpActiveTime -= Time.deltaTime;
                if(PowerUpActiveTime <= 0 )
                {
                    timerover = true;
                }
                if (timerover)
                {
                    timer = false;
                }

            }
        }
        if (timerover)
        {
            PowerUpActiveTime = StartPowerUpActiveTime;
            pm.Speed = NormalSpeed;



        }

    }
    private void Start()
    {
        PowerUpActiveTime = StartPowerUpActiveTime;
        timerover = false;
        NormalSpeed = pm.Speed;
    }




}

Try adding this at Line 53:

        timerover = false;

Try using a coroutine instead of update. So… this “Should” work, I haven’t tested it. This eliminates the need for the bool, unless you need the bool. If that’s the case you can add the bool value “true” during the “while” and then return false after the coroutine finishes.

I’ve used “PowerUpDuration” in place of your current variable. Which this should just be set to the value of how long you want the power up to last.

    public PlayerMovement pm;
    public float PowerUpDuration;
    public float peperSpeed;
    float NormalSpeed;
    float timer;


    // Start is called before the first frame update
    private void Start()
    {
        NormalSpeed = pm.Speed;
    }

    private void OnTriggerEnter2D(Collider2D col)
    {
        if (col.name == "peper")
        {
            StartCoroutine(Boost());
        }
    }

    public IEnumerator Boost()
    {

        timer = PowerUpDuration;

        while (timer != 0)
        {
            pm.Speed = peperSpeed;
            timer -= Time.deltaTime;
            yield return null;
        }

        pm.Speed = NormalSpeed;

    }
}
1 Like

This is great advice. Coroutines are the ideal choice for action that is finite in duration.

I would make this change at Line 30:

while (timer > 0)

Otherwise, at Line 33, if timer is only slightly greater than zero, subtracting Time.deltaTime from it might make it less than zero (sort of “skipping over” zero entirely), and the while loop will never end.

1 Like

Stevens, I agree about line 30!

I was typing off my head here and didn’t really think that through like I would have if coding for my own project. Glad you caught that! Thanks!

1 Like

Thank u very much I was on this problem for like 3 h

1 Like

Not a problem. Good luck with your project!

1 Like