Search Unity

NullReferenceException?

Discussion in 'Editor & General Support' started by ArcDragon, Mar 12, 2012.

  1. ArcDragon

    ArcDragon

    Joined:
    Feb 21, 2012
    Posts:
    23
    Hi.
    I want to get the Ball.cs component that is attached to the Ball object.
    And I want to change ball rigidbody velocity.
    When I wrote code.error is displayed.In C# script.

    NullReferenceException
    UnityEngine.GameObject.GetComponent[Ball] ()

    Code (csharp):
    1.  
    2. ---GameControll.cs---------
    3. void Start () {
    4.     Instantiate(Ballprefab,ballpos,Quaternion.identity);
    5. }
    6. ---ball.cs---------
    7. private Ball ball;
    8.  
    9. void Start () {
    10.     ball = GameObject.Find("Ball(Clone)").GetComponent<Ball>();
    11. }
    12. void Example() {
    13.     ball.rigidbody.velocity /= 1.2f;
    14. }
    15.  

    Please help me.
    Thank you.
     
    Last edited: Mar 12, 2012
  2. tatelax

    tatelax

    Joined:
    Feb 4, 2010
    Posts:
    1,166
    Code (csharp):
    1. ---GameControll.cs---------
    2.  
    3. void Start () {
    4.  
    5.     Instantiate(Ballprefab,ballpos,Quaternion.identity);
    6.  
    7. }
    8.  
    9. ---ball.cs---------
    10.  
    11. private Ball ball;
    12.  
    13.  
    14.  
    15. void Start () {
    16.  
    17.     ballObject = GameObject.Find("Ball(Clone)");
    18.     ball = ballObject.GetComponent<Ball>();
    19.  
    20. }
    21.  
    22. void Example() {
    23.  
    24.     ball.rigidbody.velocity /= 1.2f;
    25.  
    26. }
     
  3. ArcDragon

    ArcDragon

    Joined:
    Feb 21, 2012
    Posts:
    23
    Thank you! tatelax!
     
unityunity