Search Unity

Code not being run - (beginner!)

Discussion in 'Scripting' started by Govind-Krishna, Jan 29, 2017.

  1. Govind-Krishna

    Govind-Krishna

    Joined:
    Jul 10, 2015
    Posts:
    5
    Hi all,

    Been watching some tutorials and trying out this classic pong game and I stuck at this place where the ball is supposed to gain speed as it collides on the bat. However, the code isn't working (though there are no errors).

    So what I have done is written the function to gain speed as the ball hits the bat and its after the update code block. The script is component of the Bat gameOBJ

    I have declared a variable ballCollisionSpeed to be equal to 5.

    Code (CSharp):
    1.   void onCollisionExit2D(Collision2D ballTouch)
    2.  
    3.  {
    4.        ballTouch.rigidbody.velocity = new Vector2( ballTouch.rigidbody.velocity.x * 2.1f, ( ballTouch.rigidbody.velocity.y + ballCollisonSpeed) * Time.deltaTime);
    5.  
    6. Debug.Log(ballTouch.rigidbody.velocity);      
    7. Debug.Log("COLLIDED");
    8. print("I have collided");
    9.     }
    10.  

    The ball not only slows down after gradually (opposite of what I want), but none of Debug.Log shows up on the console, nor does the print. I tried re-arranging the code, setting it up in the ball gameOBJ but nothing seems to work. It's as if the code is not being read at all. As I commented out the code and the game doesn't seem to be affected.


    Thanks for help & regards.
    -G
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Capital O in OnCollision
     
  3. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Adding to this, all MonoBehaviour methods all start with capital letters.
     
  4. Govind-Krishna

    Govind-Krishna

    Joined:
    Jul 10, 2015
    Posts:
    5
    I feel so dumb right now...

    Thank you so much guys, that helped and it worked. I have been trying to figure it out from 2-3 hours.
     
    TaleOf4Gamers likes this.