2.5D Projectile Help

I’m working on a 2.5D game for senior project at my school. I have the script working to where it instantiates the object I am using. However, I have two issues (that may very well be related).

  1. I keep getting this error

NullReferenceException: Object reference not set to an instance of an object
PlayerAttack.Update () (at Assets/Scripts/PlayerAttack.cs:90)

I understand that it’s telling me something isn’t right at line 90, but I cannot figure out why it’s throwing the error.

  1. The force I’m adding the the projectile doesn’t seem to be working. I even disabled gravity on the rigidbody to see if that made any effect (it didn’t).

Any pointers in the right direction would be greatly appreciated!

Here is the code I am working on

#region Notes Section
/*

Author: Paul Christopher
Use: This code will:
        -Handle the different attacks(or spells) a player will have at their disposal
        -Check to see if the player is switching (or switched) to a different spell
        -Keep track of the zone the player is in and allow only a certain number of spells to be used (Functionality will be added later on)
        -Instantiate the prefab or particles to show the attack to the player

Notes: None at this time

*/
#endregion


using UnityEngine;
using System.Collections;

public class PlayerAttack : MonoBehaviour {

    #region Variables
    //Variables for different HUD based things 
    int health, magicLevel, slavesFreed, zoneNumber;
    public int attackNumber = 1;

    //Projectile speed
    float bulletSpeed = 100;
    //public Rigidbody projectile;

    //Variables to hold the different attacks
    public GameObject fireProjectile;
    //GameObject projectile;

    //Projectile Test Variables
    public GameObject projectileSpawn;
    #endregion

    #region Start
    // Use this for initialization
    void Start () {
	}
    #endregion

    #region Update
    // Update is called once per frame
    void Update ()
    {

        //Checks for keyboard input to cycle attack number down
        if (Input.GetKeyDown(KeyCode.Q))
        {
            //Cycles number as long as it is greater than zero
            if (attackNumber > 1)
            {
                attackNumber -= 1;
            }
        }

        //Checks for keyboard input to cycle attack number up
        if (Input.GetKeyDown(KeyCode.E))
        {
            //Cycles number as long as it is less than 6
            if (attackNumber < 6)
            {
                attackNumber += 1;
            }
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            //Test 1
            //Instantiate(fireProjectile, transform.position, Quaternion.identity);
            //Rigidbody rb = fireProjectile.GetComponent<Rigidbody>();
            ////rb.AddForce(500, 200, 0);
            //rb.velocity = (transform.right * -1) * bulletSpeed * Time.deltaTime;

            //Test 2
            //Rigidbody instantiateProjectile = Instantiate(fireProjectile, transform.position, transform.rotation) as Rigidbody;
            ////instantiateProjectile.velocity = transform.TransformDirection(new Vector3(500, 5, 0));
            //instantiateProjectile.AddForce(500,10,0);

            //Test 3
            //Instantiate(fireProjectile, transform.position, Quaternion.identity);
            //fireProjectile.transform.position += new Vector3(10, 0, 0);

            //Test 4
            //Rigidbody hitPlayer;
            //hitPlayer = Instantiate(fireProjectile, transform.position, transform.rotation) as Rigidbody;
            //hitPlayer.AddForce(new Vector3(-1000, 0, 0));
            ////hitPlayer.velocity = transform.TransformDirection(Vector3.forward * 500);
            //Destroy(this.gameObject);

            //Test 5
            //Transform clone;
            //clone = Instantiate(fireProjectile, transform.position, transform.rotation) as Transform;



        }

        //Handle 
        switch (attackNumber)
        {
            case 1:
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                break;
            case 5:
                break;
            case 6:
                break;
        }

    }
    #endregion

    // END OF FILE BELOW
}

The specific area I’m having an issue with is here:

 if (Input.GetKeyDown(KeyCode.Space))
            {
                //Test 1
                //Instantiate(fireProjectile, transform.position, Quaternion.identity);
                //Rigidbody rb = fireProjectile.GetComponent<Rigidbody>();
                ////rb.AddForce(500, 200, 0);
                //rb.velocity = (transform.right * -1) * bulletSpeed * Time.deltaTime;
    
                //Test 2
                //Rigidbody instantiateProjectile = Instantiate(fireProjectile, transform.position, transform.rotation) as Rigidbody;
                ////instantiateProjectile.velocity = transform.TransformDirection(new Vector3(500, 5, 0));
                //instantiateProjectile.AddForce(500,10,0);
    
                //Test 3
                //Instantiate(fireProjectile, transform.position, Quaternion.identity);
                //fireProjectile.transform.position += new Vector3(10, 0, 0);
    
                //Test 4
                //Rigidbody hitPlayer;
                //hitPlayer = Instantiate(fireProjectile, transform.position, transform.rotation) as Rigidbody;
                //hitPlayer.AddForce(new Vector3(-1000, 0, 0));
                ////hitPlayer.velocity = transform.TransformDirection(Vector3.forward * 500);
                //Destroy(this.gameObject);
    
                //Test 5
                //Transform clone;
                //clone = Instantiate(fireProjectile, transform.position, transform.rotation) as Transform;
    
    
    
            }

fireProjectile is of type “GameObject” but you are trying to get it “as Rigidbody”

The “as” operator in line 89 means basically: “Dear compiler. Please check whether the object ‘before the as’ might be actually of type ‘after the as’ and if so, just assign it as this type. If not, assign null”.

Since GameObject is not a Rigidbody, your dear compiler decided to assign null. (GameObject is a totally different class than Rigidbody. Rigidbody is a component attached to a GameObject. But so is Transform. Or any other script you add.)

What you probably wanted is a call to GetComponent instead.

hitPlayer = Instantiate(fireProjectile, transform.position, transform.rotation).GetComponent<Rigidbody>();

Alternatively, you could change the type of “fireProjectile” to be “Rigidbody” and then Instantiate would return the correct component type already.

Your second problem (force is not working) is most definetely related to this one, as it crashes at the line 90 so it never sets the force in the first place.