Search Unity

Can't find and instancied rigidbody

Discussion in 'Scripting' started by ExoAve, Jan 26, 2015.

  1. ExoAve

    ExoAve

    Joined:
    Jan 26, 2015
    Posts:
    11
    Hi guys, i'm trying to make an instancied rigidbody to move forward, however my code always return:
    NullReferenceException: Object reference not set to an instance of an object
    shoot.createProjectile () (at Assets/shoot.cs:55)
    shoot.Update () (at Assets/shoot.cs:25)

    Which means that it doesn't exist, but i created it ^^
    Here's my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class shoot : MonoBehaviour {
    5.  
    6.     public GameObject projectile;
    7.     public CharacterController player;
    8.     public float projectileSpeed;
    9.  
    10.     private Vector3 position;
    11.  
    12.  
    13.     // Use this for initialization
    14.     void Start ()
    15.     {
    16.        
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update ()
    21.     {
    22.         if (Input.GetMouseButton(1))
    23.         {
    24.             locatePosition();
    25.             createProjectile();
    26.             isHit();
    27.         }
    28.  
    29.  
    30.     }
    31.  
    32.     void locatePosition() //Get the click
    33.     {
    34.  
    35.         RaycastHit hit;
    36.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    37.  
    38.         if (Physics.Raycast(ray, out hit, 1000))//check if in camera
    39.         {
    40.             position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
    41.             Debug.Log(position); //Put in console position
    42.         }
    43.     }
    44.  
    45.     void createProjectile()
    46.     {
    47.         //rotate the player
    48.         Quaternion newRotation = Quaternion.LookRotation(position - transform.position);
    49.         newRotation.x = 0f;
    50.         newRotation.z = 0f;
    51.         transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
    52.  
    53.         Rigidbody instanceBullet;
    54.         instanceBullet = Instantiate(projectile, transform.position, newRotation) as Rigidbody;
    55.         instanceBullet.AddForce(transform.forward * 500, ForceMode.Force);
    56.     }
    57.     void isHit()
    58.     {
    59.  
    60.     }
    61. }
    Thanks for the help! :)
     
  2. Ramsdal

    Ramsdal

    Joined:
    Oct 18, 2013
    Posts:
    251
    Hmm do you have a gameobject dragged to the projectile variable in the inspector (a gameobject with a rigidbody component)?
     
  3. ExoAve

    ExoAve

    Joined:
    Jan 26, 2015
    Posts:
    11
    Yes i do, the model spawn when i right click but dont move :/
     
  4. ExoAve

    ExoAve

    Joined:
    Jan 26, 2015
    Posts:
    11
    THinking about it again, maybe not, it's a blend file i imported in unity, any idea how can i attach a rigidbody to it ?

    EDIT: K i managed to attached a rigidbody to it (via a prefab) but its still don't move and i have the same error :s any idea ?

    EDIT2: Still working on it i changed this code:

    Code (CSharp):
    1.  
    2. Rigidbody instanceBullet = Instantiate(projectile, transform.position, newRotation) as Rigidbody;
    3.         instanceBullet.velocity = position;
    And i got this error on the velocity: NullReferenceException: Object reference not set to an instance of an object which means the rigidbody/instanciate object is null ? however it still spawn in my game :s

    Thanks for your answers
     
    Last edited: Jan 26, 2015
  5. LukeWaffel

    LukeWaffel

    Joined:
    Jun 21, 2013
    Posts:
    12
    I have no idea if i'm right but this you might want to change the code to instantiate projectile as a GameObject, and then find the Rigidbody through GetComponent() Because you are currently instantiating GAMEOBJECT projectile as Rigidbody.

    Maybe something like:

    Code (CSharp):
    1. public GameObject projectile;
    Code (CSharp):
    1. GameObject instanceBullet = Instantiate(projectile, transform.position, newRotation) as GameObject;
    2.  
    3. rigidBody instanceBulletBody = instanceBullet.getComponent<"RigidBody">;
    4.  
    5. //Do stuff with the rigidbody here
    It's just psuedo code, so don't expect it to work if you copy and past it, I just quickly typed what I was thinking, didn't think about capital letters or anything.

    I hope I helped ;)
     
    PreNova likes this.
  6. Ramsdal

    Ramsdal

    Joined:
    Oct 18, 2013
    Posts:
    251
    Your initial code looked okay i think: http://docs.unity3d.com/ScriptReference/Object.Instantiate.html
    Try adding the model you want to instantiate to the hierarchy and add a rigidbody to it. Then drag the gameobject to a folder in the project explorer to make a prefab. Then add that prefab to the projectile variable in the inspector. If that does not work then???

    If it does not work try to look through this video to see where it goes wrong in the workflow - it is mostly the same thing you are looking for i thin? http://unity3d.com/learn/tutorials/modules/beginner/physics/assignments/brick-shooter
     
  7. ExoAve

    ExoAve

    Joined:
    Jan 26, 2015
    Posts:
    11
    Thanks for the help i'll try wgen i get home
     
  8. ExoAve

    ExoAve

    Joined:
    Jan 26, 2015
    Posts:
    11
    @LukeWaffel Thanks for the answer i got it! here's my final code
    Code (CSharp):
    1. GameObject instanceBullet = Instantiate(projectile, transform.position, newRotation) as GameObject;
    2.         Rigidbody instanceBulletBody;
    3.         instanceBulletBody = instanceBullet.GetComponent<Rigidbody>();
    4.         instanceBulletBody.AddForce(transform.forward, ForceMode.Force);
    Thank for the answers! :D
     
    PreNova likes this.
  9. LukeWaffel

    LukeWaffel

    Joined:
    Jun 21, 2013
    Posts:
    12
    Ahh no problem @ExoAve , I'm glad I could help! :D
     
  10. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    It's important to note the difference between "as X" and casting to X. When you do this:

    Code (CSharp):
    1. Rigidbody instanceBullet = Instantiate(projectile, transform.position, newRotation) as Rigidbody;
    and what you're instantiating can't be cast to Rigidbody, the code will silently return null. Had you instead done this:

    Code (CSharp):
    1. Rigidbody instanceBullet = (Rigidbody) Instantiate(projectile, transform.position, newRotation);
    you would have gotten an exception telling you that the cast was impossible. I'd recommend staying away from using "as", unless the code is supposed to not crash if the cast isn't possible.
     
    LukeWaffel likes this.