Beginner here failing at Debug.Log.

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.

{

public GameObject Ball;

public Rigidbody2D ballRB;





void move() { // also tried update.

if (ballRB.velocity.x > 0) { // also tried position.

Debug.Log ("Hey") }

}

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.

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.

public Rigidbody2D Ballrb;

versus

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.

1 Like

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

And? Have you run it?

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.

public class Ball : MonoBehaviour
{

    public float difficultyMultiplier = 1.13f;

    public float minXSpeed = 0.8f;
    public float maxXSpeed = 1.2f;

    public float minYSpeed = 0.8f;
    public float maxYSpeed = 1.2f;

    private Rigidbody2D ballRigidbody;

    // Use this for initialization
    void Start()
    {
        ballRigidbody = GetComponent<Rigidbody2D>();
        ballRigidbody.velocity = new Vector2(
            Random.Range(minXSpeed, maxXSpeed) * (Random.value > 0.5f ? -1 : 1),
            Random.Range(minYSpeed, maxYSpeed) * (Random.value > 0.5f ? -1 : 1)
        );
    }

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

{

    public GameObject Ball;
    public Rigidbody2D ballrb2d;
    public GameObject GOPlayer2;
    public Rigidbody2D rb2dPlayer2;
    public float Speed = 300f;
    [Space]
    [Space]
    public float ReactionTimeMIN = 0.07f;
    public float ReactionTimeMAX = 0.10f;


    void Start()
    {
        //Continuously Invokes Move every x seconds (values may differ)
        InvokeRepeating("Move", ReactionTimeMIN, ReactionTimeMAX);
    }

    // Movement for the paddle
    void Move()
    {
        //checking x direction of the ball
        if (ballrb2d.velocity.x > 0.0f)
        {
            //checking y direction of ball
            if (Ball.transform.position.y < GOPlayer2.transform.position.y - 1f)
            {
                //move ball down if lower than paddle
                rb2dPlayer2.velocity = -transform.up * Speed * Time.deltaTime;
            }
            else if (Ball.transform.position.y > GOPlayer2.transform.position.y + 1f)
            {
                //move ball up if higher than paddle
                float movementDirection = 1f * Speed * Time.deltaTime;
                rb2dPlayer2.velocity = new Vector2(0, movementDirection);
            }
        }
    }
}

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.

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:

void Start() {
    // Kick off this method.
    StartCoroutine(MoveRepeatedly());
}

private IEnumerable MoveRepeatedly() {
    // Wait a bit before starting.
    yield return new WaitForSeconds(ReactionTimeMIN);

    // From now on, every ReactionTimeMAX, call Move().
    while (true) {
        Move();
        yield return new WaitForSeconds(ReactionTimeMAX);
    }
}

private void Move() {
    // Do your move code here.
}

Again, the takeaway here is that you need to call the methods somehow. Your Move() method was never called in your original code.

So I attempted plastering Debug.Log on the ball script itself, which worked perfectly, obviously.

What I can’t understand is why this code:

{
public class PaddleBot : MonoBehaviour
    public GameObject Ball;
    public Rigidbody2D ballrb2d;

    void Start()
    {
        ballrb2d = GetComponent<Rigidbody2D>();
        if (ballrb2d.velocity.x > 0.0f)
            Debug.Log("hey");
    }

Doesn’t work. Am I trying to access the ball wrong way?

Velocity at start is 0

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? :confused:

?

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

       if (ballrb2d.velocity.x > 0.0f)
            Debug.Log("moving");
       if (ballrb2d.velocity.x == 0.0f)  // stationary or moving less than -1e5f
            Debug.Log("stationary");
{
    public float difficultyMultiplier = 1.13f;

    public float minXSpeed = 0.8f;
    public float maxXSpeed = 1.2f;

    public float minYSpeed = 0.8f;
    public float maxYSpeed = 1.2f;

    private Rigidbody2D ballRigidbody;

    // Use this for initialization
    void Start()
    {
        ballRigidbody = GetComponent<Rigidbody2D>();
        ballRigidbody.velocity = new Vector2(
            Random.Range(minXSpeed, maxXSpeed) * (Random.value > 0.5f ? -1 : 1),
            Random.Range(minYSpeed, maxYSpeed) * (Random.value > 0.5f ? -1 : 1)
        );
    }

This is the code of the ball prefab which I put in, I’m searching for that objetct’s velocity.

  • public GameObject Ball;
  • public Rigidbody2D ballrb2d;

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.

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.

{
    public float difficultyMultiplier = 1.13f;
    public float minXSpeed = 0.8f;
    public float maxXSpeed = 1.2f;
    public float minYSpeed = 0.8f;
    public float maxYSpeed = 1.2f;
    private Rigidbody2D ballRigidbody;
    // Use this for initialization
    void Start()
    {
        ballRigidbody = GetComponent<Rigidbody2D>();
        ballRigidbody.velocity = new Vector2(
            Random.Range(minXSpeed, maxXSpeed) * (Random.value > 0.5f ? -1 : 1),
            Random.Range(minYSpeed, maxYSpeed) * (Random.value > 0.5f ? -1 : 1)
        );
    }

This is the code of the ball, which has velocity applied upon start.

{
public class PaddleBot : MonoBehaviour
    public GameObject Ball;
    public Rigidbody2D ballrb2d;
    void Start()
    {
        ballrb2d = GetComponent<Rigidbody2D>();
        if (ballrb2d.velocity.x > 0.0f)
            Debug.Log("hey");
    }

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.

It could also be moving left, which means ballrb2d.velocity.x is LESS than zero
mb try ballrb2d.velocity.magnitude

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)

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?

Is the paddlebot script actaully on the BALL?
ballrb2d = GetComponent(); Gets the rb2d on the same object as the script is attatched to