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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Beginner here failing at Debug.Log.

Discussion in 'Scripting' started by F1stBr34k3r, Nov 20, 2018.

  1. F1stBr34k3r

    F1stBr34k3r

    Joined:
    Sep 17, 2018
    Posts:
    9
    I placed the prefabs where they need to be, I'm going to start a proper unity course shortly after I finish the one for c# made by Brackeys. This is part of a code shown to me by someone else.
    Code (CSharp):
    1. {
    2.  
    3. public GameObject Ball;
    4.  
    5. public Rigidbody2D ballRB;
    6.  
    7.  
    8.  
    9.  
    10.  
    11. void move() { // also tried update.
    12.  
    13. if (ballRB.velocity.x > 0) { // also tried position.
    14.  
    15. Debug.Log ("Hey") }
    16.  
    17. }
    I have no idea how I'm failing at something so simple.

    The ball moves, the script is there, the prefabs are placed on the Paddle A.I., I have no idea why I can't even get a response.
     
    Last edited: Nov 20, 2018
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    Please put your code in code tags on the forum. You can edit your post at the bottom of the post.

    In C# everything is case sensitive.
    Code (CSharp):
    1. public Rigidbody2D Ballrb;
    versus
    Code (CSharp):
    1. if (ballrb.velocity.x > 0) {
    notice Ballrb versus ballrb

    Also put the ; after the Debug.Log()...

    When something does not work, you should take a look at the console window, it should tell you (in this case with red text) what you have done wrong.
    https://docs.unity3d.com/Manual/Console.html
     
    Antypodish likes this.
  3. F1stBr34k3r

    F1stBr34k3r

    Joined:
    Sep 17, 2018
    Posts:
    9

    I posted a pic showing the entire thing, no error in console.
     

    Attached Files:

    Last edited: Nov 20, 2018
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    And? Have you run it?
     
  5. F1stBr34k3r

    F1stBr34k3r

    Joined:
    Sep 17, 2018
    Posts:
    9
    Yup, also attempted to change "<' to a bunch of other things as you can see, and the velocity isn't shown as 0.

    Here's the ball code, because I can't think of many other things that can cause conflict.

    Code (CSharp):
    1. public class Ball : MonoBehaviour
    2. {
    3.  
    4.     public float difficultyMultiplier = 1.13f;
    5.  
    6.     public float minXSpeed = 0.8f;
    7.     public float maxXSpeed = 1.2f;
    8.  
    9.     public float minYSpeed = 0.8f;
    10.     public float maxYSpeed = 1.2f;
    11.  
    12.     private Rigidbody2D ballRigidbody;
    13.  
    14.     // Use this for initialization
    15.     void Start()
    16.     {
    17.         ballRigidbody = GetComponent<Rigidbody2D>();
    18.         ballRigidbody.velocity = new Vector2(
    19.             Random.Range(minXSpeed, maxXSpeed) * (Random.value > 0.5f ? -1 : 1),
    20.             Random.Range(minYSpeed, maxYSpeed) * (Random.value > 0.5f ? -1 : 1)
    21.         );
    22.     }
     

    Attached Files:

    • wot.png
      wot.png
      File size:
      527.6 KB
      Views:
      575
  6. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    The function "move"(BTW function should be capitalized, just a consistency thing it's not mandatory or something)
    even if it does work, you are not calling it anywhere, the function Update is a magic-unity-function that runs once a frame update(you have FixedUpdate that runs everytime the physics engine does a tick and many others, see the MonoBehaviour API), that's why you don't need to call it yourself

    also, velocity and any other vector 3 is made up of 3 floats, if you don't know the difference between float and int: int is whole nubmers(IE 0,1,2,3..) and float is a number with decimals(floating point, IE 0.3,7.5,2.0...), it may be a casting(converting) issue between them, because you are comparing the y value(float) to 0(int), you need to say "0f" to say it's a float, better to say 0.0f, but you need the "f" there to say it's a float
     
  7. F1stBr34k3r

    F1stBr34k3r

    Joined:
    Sep 17, 2018
    Posts:
    9
    Code (CSharp):
    1. {
    2.  
    3.     public GameObject Ball;
    4.     public Rigidbody2D ballrb2d;
    5.     public GameObject GOPlayer2;
    6.     public Rigidbody2D rb2dPlayer2;
    7.     public float Speed = 300f;
    8.     [Space]
    9.     [Space]
    10.     public float ReactionTimeMIN = 0.07f;
    11.     public float ReactionTimeMAX = 0.10f;
    12.  
    13.  
    14.     void Start()
    15.     {
    16.         //Continuously Invokes Move every x seconds (values may differ)
    17.         InvokeRepeating("Move", ReactionTimeMIN, ReactionTimeMAX);
    18.     }
    19.  
    20.     // Movement for the paddle
    21.     void Move()
    22.     {
    23.         //checking x direction of the ball
    24.         if (ballrb2d.velocity.x > 0.0f)
    25.         {
    26.             //checking y direction of ball
    27.             if (Ball.transform.position.y < GOPlayer2.transform.position.y - 1f)
    28.             {
    29.                 //move ball down if lower than paddle
    30.                 rb2dPlayer2.velocity = -transform.up * Speed * Time.deltaTime;
    31.             }
    32.             else if (Ball.transform.position.y > GOPlayer2.transform.position.y + 1f)
    33.             {
    34.                 //move ball up if higher than paddle
    35.                 float movementDirection = 1f * Speed * Time.deltaTime;
    36.                 rb2dPlayer2.velocity = new Vector2(0, movementDirection);
    37.             }
    38.         }
    39.     }
    40. }
    This is the whole code as given to me, I simply removed everything to try and invoke a response from Debug.Log, tried changing 0 to 0.0f too, still no answer. Also, should probably have figured not to remove the Move mentioned above, but I wanted the minimum of code possible to try and invoke a reaction and... nothing.

    I think I'm making this more complicated as I go.
     
  8. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,113
    Note the `InvokeRepeating` call in the Start method of your full code. That's causing the Move function to be called every .1 seconds. Your modified version of this code didn't include the InvokeRepeating call, so nothing was calling your method.

    This is honestly the first I've seen of InvokeRepeating. I don't know why I'd ever use this over a much more clear Coroutine approach.

    Anyway, here's the Coroutine equivalent of what you're trying to do:

    Code (CSharp):
    1. void Start() {
    2.     // Kick off this method.
    3.     StartCoroutine(MoveRepeatedly());
    4. }
    5.  
    6. private IEnumerable MoveRepeatedly() {
    7.     // Wait a bit before starting.
    8.     yield return new WaitForSeconds(ReactionTimeMIN);
    9.  
    10.     // From now on, every ReactionTimeMAX, call Move().
    11.     while (true) {
    12.         Move();
    13.         yield return new WaitForSeconds(ReactionTimeMAX);
    14.     }
    15. }
    16.  
    17. private void Move() {
    18.     // Do your move code here.
    19. }
    Again, the takeaway here is that you need to call the methods somehow. Your Move() method was never called in your original code.
     
  9. F1stBr34k3r

    F1stBr34k3r

    Joined:
    Sep 17, 2018
    Posts:
    9
    So I attempted plastering Debug.Log on the ball script itself, which worked perfectly, obviously.

    What I can't understand is why this code:

    Code (CSharp):
    1. {
    2. public class PaddleBot : MonoBehaviour
    3.     public GameObject Ball;
    4.     public Rigidbody2D ballrb2d;
    5.  
    6.     void Start()
    7.     {
    8.         ballrb2d = GetComponent<Rigidbody2D>();
    9.         if (ballrb2d.velocity.x > 0.0f)
    10.             Debug.Log("hey");
    11.     }
    Doesn't work. Am I trying to access the ball wrong way?
     
  10. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Velocity at start is 0
     
  11. F1stBr34k3r

    F1stBr34k3r

    Joined:
    Sep 17, 2018
    Posts:
    9
    So... this was a good example of how to look at things differently.

    I changed it to equal, and it worked.

    Then I made sure to get constant updates, and... it worked again, meaning that the ball always sends the message that it's at 0 velocity?

    Now I need to figure out how can it be stationary and moving at the same time? :/
     
  12. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    ?

    The piece of code does not cause to move and be stationary. It detects if the ball has valocity on x or not.

    Code (CSharp):
    1.        if (ballrb2d.velocity.x > 0.0f)
    2.             Debug.Log("moving");
    3.        if (ballrb2d.velocity.x == 0.0f)  // stationary or moving less than -1e5f
    4.             Debug.Log("stationary");
    5.  
     
  13. F1stBr34k3r

    F1stBr34k3r

    Joined:
    Sep 17, 2018
    Posts:
    9
    Code (CSharp):
    1. {
    2.     public float difficultyMultiplier = 1.13f;
    3.  
    4.     public float minXSpeed = 0.8f;
    5.     public float maxXSpeed = 1.2f;
    6.  
    7.     public float minYSpeed = 0.8f;
    8.     public float maxYSpeed = 1.2f;
    9.  
    10.     private Rigidbody2D ballRigidbody;
    11.  
    12.     // Use this for initialization
    13.     void Start()
    14.     {
    15.         ballRigidbody = GetComponent<Rigidbody2D>();
    16.         ballRigidbody.velocity = new Vector2(
    17.             Random.Range(minXSpeed, maxXSpeed) * (Random.value > 0.5f ? -1 : 1),
    18.             Random.Range(minYSpeed, maxYSpeed) * (Random.value > 0.5f ? -1 : 1)
    19.         );
    20.     }
    This is the code of the ball prefab which I put in, I'm searching for that objetct's velocity.

    1. public GameObject Ball;
    2. public Rigidbody2D ballrb2d;
     
  14. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,113
    Can you explain what your question is at this point?

    As LurkingNinjaDev pointed out, in response to your earlier statement, your previous code wasn't checking whether the object was "stationary". It was merely checking whether it was not moving left or right. The object could have been moving up/down, but you were only checking the x-component of its velocity.

    If you want to check that the object is not moving, you'd need to check velocity.x and velocity.y are both 0.
     
  15. F1stBr34k3r

    F1stBr34k3r

    Joined:
    Sep 17, 2018
    Posts:
    9
    The ball, as shown in the script above, has both a random X and a random Y value that happens upon execution, which causes it to move.

    The paddle, does not detect any movement.

    Code (CSharp):
    1. {
    2.     public float difficultyMultiplier = 1.13f;
    3.     public float minXSpeed = 0.8f;
    4.     public float maxXSpeed = 1.2f;
    5.     public float minYSpeed = 0.8f;
    6.     public float maxYSpeed = 1.2f;
    7.     private Rigidbody2D ballRigidbody;
    8.     // Use this for initialization
    9.     void Start()
    10.     {
    11.         ballRigidbody = GetComponent<Rigidbody2D>();
    12.         ballRigidbody.velocity = new Vector2(
    13.             Random.Range(minXSpeed, maxXSpeed) * (Random.value > 0.5f ? -1 : 1),
    14.             Random.Range(minYSpeed, maxYSpeed) * (Random.value > 0.5f ? -1 : 1)
    15.         );
    16.     }
    This is the code of the ball, which has velocity applied upon start.

    Code (CSharp):
    1. {
    2. public class PaddleBot : MonoBehaviour
    3.     public GameObject Ball;
    4.     public Rigidbody2D ballrb2d;
    5.     void Start()
    6.     {
    7.         ballrb2d = GetComponent<Rigidbody2D>();
    8.         if (ballrb2d.velocity.x > 0.0f)
    9.             Debug.Log("hey");
    10.     }
    And this is the code of the paddle, where I want it to detect the value from the code above.

    I'm just trying to have it tell me "That object you told me about is moving", but it's telling me that it's not moving for some reason, even though in the unity component itself, the velocity clearly shows that x != 0.

    The ball is moving in both Y and X, I just want to see if it figures out that the X changes, and the editor shows that it does. But it only answers when I change the sign to ==, which means that it doesn't detect any movement, even though the ball is constantly moving in both axis.
     
  16. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    It could also be moving left, which means ballrb2d.velocity.x is LESS than zero
    mb try ballrb2d.velocity.magnitude
     
  17. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Better to use the square magnitude if you dont need the actual distance

    If you still want to check only one axis you can do Mathf.Abs() to get the absolute value(distance from 0, always positive, or 0)
     
  18. F1stBr34k3r

    F1stBr34k3r

    Joined:
    Sep 17, 2018
    Posts:
    9
    It spawns into a random direction, and it's also bouncing left and right, so shouldn't it provide a message in at least one of the directions?

    At any rate, this one didn't work.

    Well... It returns the value of 0, but again, the ball is moving, so why?
     
  19. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Is the paddlebot script actaully on the BALL?
    ballrb2d = GetComponent<Rigidbody2D>(); Gets the rb2d on the same object as the script is attatched to