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

Pong ball / Void awake

Discussion in 'Scripting' started by CodeWurm, Dec 21, 2015.

  1. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    My code does not work, anyone can help I want to move my ball when I test my game.
    I'm making a pong game.
    I'm also using a selfmade ball, not made with unity.

    Here is my code:
    Code (CSharp):
    1. public class Ball : MonoBehaviour {
    2.     void awake()
    3.                    
    4.     {
    5.         GetComponent<Rigidbody>().AddForce (4, 0, 0, ForceMode.Impulse);
    6.        
    7.     }
    8. }
     
  2. mikeymike

    mikeymike

    Joined:
    Oct 21, 2014
    Posts:
    35
    captial A for Awake. makes all the difference
     
  3. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    It says there is no rigidbody attached to the gameobject.
    Do I need to use rigidbody? Is there another way to tell what to do with my Ball gameobject.
     
  4. mikeymike

    mikeymike

    Joined:
    Oct 21, 2014
    Posts:
    35
    if you want to use AddForce, you need a rigidbody, just add one in the inspector or add one in your script
    Code (csharp):
    1.  
    2. void Awake(){
    3. Rigidbody rg = this.AddComponent("Rigidbody") as Rigidbody;
    4. rg.AddForce(4, 0, 0, ForceMode.Impulse);
    5. }
    6.  
    sounds like you need to look at some tutorials first and foremost though.