Search Unity

Wait until a variable changed

Discussion in 'Scripting' started by Schoeneberg, Aug 3, 2020.

  1. Schoeneberg

    Schoeneberg

    Joined:
    Jul 28, 2020
    Posts:
    11
    Hello guys,
    I need help. I have a code in which i will wait until the variable jumpspeed changed into the same value of JumpCounter which is a variable of a other script. Right now the variable jumpspeed changed right but to late so when I use the variable in game the variable isnt changed because it changed a litle bit later. So I have to wait in the script until the variable jumpspeed is higher then 0. How can I do this?

    Here is my code in the first object in which I will wait:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Collections.Specialized;
    4. using System.Diagnostics;
    5. using UnityEngine;
    6.  
    7. public class PlayerMovement : MonoBehaviour
    8. {
    9.     public float jumpspeed;
    10.     bool canJump;
    11.     Rigidbody2D myrb;
    12.  
    13.     void Start()
    14.     {
    15.         myrb = GetComponent<Rigidbody2D> ();
    16.     }
    17.  
    18.     public void Jump()                                                         // Die Funktion zum springen
    19.     {
    20.         if (canJump == true)
    21.         {
    22.             canJump = false;
    23.             myrb.velocity = new Vector2(0f, jumpspeed);
    24.             UnityEngine.Debug.Log("Ich kann springen mit " + jumpspeed);
    25.         }
    26.         UnityEngine.Debug.Log("Ich kann nicht springen mit " + jumpspeed);
    27.     }
    28.     void OnCollisionEnter2D(Collision2D other)                                 // Die Funktion zum Testen, ob man springen darf
    29.     {
    30.         canJump = true;
    31.     }
    32. }
    and here is my code of the other object in which is the variable JumpCounter which change the variable JumpCounter

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class JumpCount : MonoBehaviour
    7. {
    8.     public float JumpCounter;
    9.     float startTime;
    10.     PlayerMovement myPlayerMovement;
    11.  
    12.     void Start()
    13.     {
    14.         myPlayerMovement = GameObject.FindObjectOfType<PlayerMovement>();
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         if (Input.GetMouseButtonDown(0))
    20.         {
    21.             startTime = Time.time;
    22.         }
    23.         if (Input.GetMouseButtonUp(0))
    24.         {
    25.             JumpCounter = Time.time - startTime;
    26.             JumpCounter *= 15f;
    27.             myPlayerMovement.jumpspeed = JumpCounter;
    28.             Debug.Log("JumpCounter " + JumpCounter);
    29.         }
    30.     }
    31. }
    I hope really you can help me
     
  2. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    Hi ich hab gemerkt das du deutsch in deinem Script geschreiben hast also anworte ich dir einfach mal auf deutsch. Du hast zwar deine Funktion "Jump" aber du rufst sie nie auf, weshalb du warscheinlich auch keine der Debug.Log Nachriten erhalten hast, richtig?
     
  3. Schoeneberg

    Schoeneberg

    Joined:
    Jul 28, 2020
    Posts:
    11
    Leider doch weil ich sie in der On Click() Funktion des Buttons aktiviere
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    I think you want this:

    Code (csharp):
    1. private float jumpTimer;
    and in Update():

    Code (csharp):
    1. jumpTimer += Time.deltaTime;
    2.  
    3. if (jumpTimer > 0.5f) // half of one second
    4. {
    5.    // do something
    6.    jumpTimer = 0.0f;  // reset
    7. }
    I am not sure 100% if you are inhibiting or enabling jump based on the timer.
     
  5. Schoeneberg

    Schoeneberg

    Joined:
    Jul 28, 2020
    Posts:
    11
    Not exactly because I dont want to do something when a time is over I want to do something when the Button is pressed so the function Jump() is activated and jumpspeed is higher then 0. I think it is easy but I dont get it.
     
  6. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    Call me crazy but in your script the variable jumpspeed is not changed at all (means there is no new value assigned). Only the value which is set in the inspector is applied. If you expect it to change you have to assign a new value. But I don't really understand what the issue is.

    if( jumpspeed > 0.0f)
     
  7. Schoeneberg

    Schoeneberg

    Joined:
    Jul 28, 2020
    Posts:
    11
    I change the variable jumpspeed in the other skript which is in the button. Thats the second script I show on top.

    Now I changed my first into this but it does not work.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Collections.Specialized;
    4. using System.Diagnostics;
    5. using UnityEngine;
    6.  
    7. public class PlayerMovement : MonoBehaviour
    8. {
    9.     public float jumpspeed;
    10.     bool canJump;
    11.     Rigidbody2D myrb;
    12.  
    13.     void Start()
    14.     {
    15.         myrb = GetComponent<Rigidbody2D> ();
    16.     }
    17.  
    18.     public void Jump()                                                         // Die Funktion zum springen
    19.     {
    20.         while (canJump == true)
    21.         {
    22.             if (jumpspeed > 0.0f)
    23.             {
    24.                 canJump = false;
    25.                 myrb.velocity = new Vector2(0f, jumpspeed);
    26.                 UnityEngine.Debug.Log("Ich kann springen mit " + jumpspeed);
    27.             }
    28.         }
    29.         UnityEngine.Debug.Log("Ich kann nicht springen mit " + jumpspeed);
    30.     }
    31.     void OnCollisionEnter2D(Collision2D other)                                 // Die Funktion zum Testen, ob man springen darf
    32.     {
    33.         canJump = true;
    34.     }
    35. }
     
  8. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    This is usually considered "bad style". You should not change the state of other objects directly. Better is it to call methods on said object which change the state accordingly. Just imagine you have to check for validity or you need to change several values at once in a certain order. If you forget this in one place of (potentially) many places this leaves you with an object in an invalid state. Thus you have a method to take care of this and other scripts are like "customers" which just call the method and the method takes care that everything is handled properly.
    So instead of
    Code (csharp):
    1.  
    2. myPlayerMovement.jumpspeed = JumpCounter;
    3.  
    you should use something like
    Code (csharp):
    1.  
    2. myPlayerMovement.SetJumpSpeed(JumpCounter);
    3.  
    Even a property would do in this case.

    Code (csharp):
    1.  
    2. while (canJump == true)
    3.  
    This will probably create an infinte loop and crash your editor. This prevents your frame from proceeding. But as I said it's not entirely clear to me what you want to achieve with this. Shall the player only be able to jump when he is on ground? Because you have a collision which sets canJump to true. If so you should check if other is really the ground and not an arbitrary object. And why should the player can initiate the jump BEFORE he is at ground (as you say you want to wait for it)? You could disable the button as long as he is not on ground or play a sound to indicate that. If you want to check every frame instead of the while loop check out coroutines or even a simple update would do. But if you do this the player can press jump while not on the ground and he jumps again as soon as he reaches the ground. I think this is not what players expect. If I'm wrong you should explain in more detail what you want to achieve.[/code]
     
  9. Schoeneberg

    Schoeneberg

    Joined:
    Jul 28, 2020
    Posts:
    11
    Thanks for youre answear in detail. I think I dont explain it well, sorry. The code isnt so clean and good because I am new to Unity and my english isnt so good because I am student in the 10th class. In particular I want a charge jump which is churging in a variable of a other script this variable is JumpCounter then I want that jumpspeed get this value and use it before the player jump. Now I got it, I only have to change the void Jump which activates with the On Click() method of the button into void update() and add jumpspeed > 0 in the if question. Here is my working code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Collections.Specialized;
    4. using System.Diagnostics;
    5. using UnityEngine;
    6.  
    7. public class PlayerMovement : MonoBehaviour
    8. {
    9.     public float jumpspeed;
    10.     bool canJump;
    11.     Rigidbody2D myrb;
    12.  
    13.     void Start()
    14.     {
    15.         myrb = GetComponent<Rigidbody2D> ();
    16.     }
    17.  
    18.     void Update()                                                         // Die Funktion zum springen
    19.     {
    20.         if (canJump == true && jumpspeed > 0)
    21.         {
    22.             canJump = false;
    23.             myrb.velocity = new Vector2(0f, jumpspeed);
    24.             jumpspeed = 0;
    25.         }
    26.     }
    27.     void OnCollisionEnter2D(Collision2D other)                                 // Die Funktion zum Testen, ob man springen darf
    28.     {
    29.         canJump = true;
    30.     }
    31. }
    I hope I dont rob to much time from you by helping me. I am very happy and grateful becouse I only got it with some ideas which I got because of youre answears. So thanks and have a nice day.
     
  10. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    If so I wouldn't bother to help. So it's fine. And I'm glad you got it working as intended. Keep experimenting and reading (tutorials, forums). This widens your mental horizon regarding game development. And if you need help or advise try to forumlate your issues and questions (and desired goal) as clearly as possible. Often when you try to explain something to someone else this makes things more clear for yourself too. Good luck with your endeavours.