Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

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

Discussion in 'Scripting' started by thefrogeitan, Oct 17, 2020.

  1. thefrogeitan

    thefrogeitan

    Joined:
    Oct 20, 2019
    Posts:
    7
    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
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     private float TargetPos;
    8.     public float Speed;
    9.  
    10.  
    11.     void Start()
    12.     {
    13.  
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.        
    20.         transform.position = new Vector2(TargetPos, transform.position.y);
    21.     }
    22.  
    23.     public void right()
    24.     {
    25.         TargetPos = transform.position.x + Speed * Time.deltaTime;
    26.     }
    27.     public void left()
    28.     {
    29.         TargetPos = transform.position.x - Speed * Time.deltaTime;
    30.     }
    31. }
    32.  
    33.    
    34.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Powrups : MonoBehaviour
    6. {
    7.  
    8.     public PlayerMovement pm;
    9.     public float PowerUpActiveTime;
    10.     public float StartPowerUpActiveTime;
    11.     public float peperSpeed;
    12.     float NormalSpeed;
    13.     bool timer;
    14.     bool timerover;
    15.  
    16.  
    17.     private void OnTriggerEnter2D(Collider2D col)
    18.     {
    19.         if (col.name == "peper")
    20.         {
    21.             pm.Speed = peperSpeed;
    22.             timer = true;
    23.             Debug.Log("timerOn");
    24.             Debug.Log(pm.Speed);
    25.             Debug.Log(timer);
    26.  
    27.         }
    28.     }
    29.     private void Update()
    30.     {
    31.         while(timer)
    32.         {
    33.             GameObject Pause = GameObject.Find("Pause");
    34.             PauseScript pausescript = Pause.GetComponent<PauseScript>();
    35.             if (!pausescript.pause)
    36.             {
    37.                 PowerUpActiveTime -= Time.deltaTime;
    38.                 if(PowerUpActiveTime <= 0 )
    39.                 {
    40.                     timerover = true;
    41.                 }
    42.                 if (timerover)
    43.                 {
    44.                     timer = false;
    45.                 }
    46.  
    47.             }
    48.         }
    49.         if (timerover)
    50.         {
    51.             PowerUpActiveTime = StartPowerUpActiveTime;
    52.             pm.Speed = NormalSpeed;
    53.  
    54.  
    55.  
    56.         }
    57.  
    58.     }
    59.     private void Start()
    60.     {
    61.         PowerUpActiveTime = StartPowerUpActiveTime;
    62.         timerover = false;
    63.         NormalSpeed = pm.Speed;
    64.     }
    65.  
    66.  
    67.  
    68.  
    69. }
    70.  
     

    Attached Files:

  2. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    Try adding this at Line 53:
    Code (CSharp):
    1.         timerover = false;
     
  3. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    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.

    Code (csharp):
    1.  
    2.  
    3.  
    4.     public PlayerMovement pm;
    5.     public float PowerUpDuration;
    6.     public float peperSpeed;
    7.     float NormalSpeed;
    8.     float timer;
    9.  
    10.  
    11.     // Start is called before the first frame update
    12.     private void Start()
    13.     {
    14.         NormalSpeed = pm.Speed;
    15.     }
    16.  
    17.     private void OnTriggerEnter2D(Collider2D col)
    18.     {
    19.         if (col.name == "peper")
    20.         {
    21.             StartCoroutine(Boost());
    22.         }
    23.     }
    24.  
    25.     public IEnumerator Boost()
    26.     {
    27.  
    28.         timer = PowerUpDuration;
    29.  
    30.         while (timer != 0)
    31.         {
    32.             pm.Speed = peperSpeed;
    33.             timer -= Time.deltaTime;
    34.             yield return null;
    35.         }
    36.  
    37.         pm.Speed = NormalSpeed;
    38.  
    39.     }
    40. }
    41.  
     
    Stevens-R-Miller likes this.
  4. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    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.
     
    Boo-Let likes this.
  5. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    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!
     
    Stevens-R-Miller likes this.
  6. thefrogeitan

    thefrogeitan

    Joined:
    Oct 20, 2019
    Posts:
    7
    Thank u very much I was on this problem for like 3 h
     
    Stevens-R-Miller likes this.
  7. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Not a problem. Good luck with your project!
     
    Stevens-R-Miller likes this.