Search Unity

Getting access to instantiated rigidbody c#

Discussion in 'Scripting' started by jococo, Apr 18, 2015.

  1. jococo

    jococo

    Joined:
    Dec 15, 2012
    Posts:
    232
    Hello,

    I need to get access to an instantiated prefab that contains a rigidbody to add some force to it in c#.


    Code (CSharp):
    1.  
    2. // Script 1 Instantiates it ...
    3. void Start()
    4. {
    5.       GameObject fireBall = Instantiate( "FireBall", newVector3( 0, 0, 0), Quaternion.identity, 0 );
    6. }
    7.  
    8.  
    9.  
    10. // Script 2 needs to access it ...
    11. Rigidbody fireBallRigidBody = (Rigidbody)GetComponent<Rigidbody>();
    12.  
    13. if(fireBallRigidBody == null)
    14. {
    15.     Debug.Log ("fireBallRigidBody is null ");
    16. }
    Pretty simply I use to think but the rigidbody continues to be null and I'm apparently rusty ...

    I've done this before but drawing a blank and can't seem to find a good example.

    Hint?

    Thanks
     
  2. jococo

    jococo

    Joined:
    Dec 15, 2012
    Posts:
    232
    And of course as soon as I post to ask I figure it out.

    Code (CSharp):
    1.  
    2. fireBallRigidBody = fireBall.GetComponent<Rigidbody>();
    3.  
     
  3. proandrius

    proandrius

    Unity Technologies

    Joined:
    Dec 4, 2012
    Posts:
    544
    Just make sure you do that in Start as well. :)