The question is answered, right answer was accepted
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.
2) 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;
}
so in which Test the error . but as i see you are trying to add force or velocity to fireProjectile from this code much better way that you attach a script to fireProjectile which contain movment code ins$$anonymous$$d so it will move once it been instantiated . try Test 1 with one line of code :
Instantiate(fireProjectile, transform.position, Quaternion.identity);
& in fireProjectile script :
Rigidbody rb;
float bulletSpeed = 100;
void Start(){
rb = GetComponent<Rigidbody>();
}
void Update(){
rb.velocity = new Vector3(bulletSpeed * Time.deltaTime, 0, 0);
}
I'll give that a shot and see if it works! Thank you so much for the input
Also, the error seems to occur in all of the tests. For some reason as soon as the code is executed, the value becomes null for the projectile prefab I am trying to instantiate. Then when I set it, it still throws the error, so I'm not sure what is going on there.
This seems to work, the only issue I'm getting is that I now need to figure out how to get it to shoot in front of my model depending if it is moving left or right. Thanks for the input!
Answer by Immanuel-Scholz · Apr 14, 2016 at 02:26 PM
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.
Thanks for the input. That definitely makes sense. I don't have a ton of dedicated program$$anonymous$$g experience with instantiating through Unity, so this is a bit of a learning experience too. I will make any edits as soon as I am home from class and see if that fixes the issues I am having!