- Home /
object referance problem with bullet adding
i keep getting the following error NullReferenceException: Object reference not set to an instance of an object heatSeakingProjectile.Update () (at Assets/Scripts/GamePlay/heatSeakingProjectile.cs:27)
void Update ()
{
if (Input.GetMouseButton (0)) {
foreach (Transform child in transform) {
if (child.tag == "heatSeak") {
Rigidbody clone;
clone = Instantiate (Resources.Load ("gamePlay/heatSeak"), child.position, child.rotation)as Rigidbody;
print(clone.velocity);// error occurring here
}
}
}
}
Answer by GuyTidhar · Apr 13, 2012 at 08:22 AM
This line of code is where the error actually start:
clone = Instantiate (Resources.Load ("gamePlay/heatSeak"), child.position, child.rotation)as Rigidbody;
What type of asset are you trying to instantiate? Isn't heatSeak a Game Object?
Could it be that heatSeak is a gameObject with a RigidBody on it?
I also suggest you do this:
private GameObject heatSeakPrefab;
void Start()
{
heatSeakPrefab = Resources.Load ("gamePlay/heatSeak", typeof(GameObject)) as GameObject;
}
void Update()
{
if (Input.GetMouseButton (0))
{
foreach (Transform child in transform)
{
if (child.CompareTag("heatSeak") )
{
Rigidbody clone;
clone = Instantiate (heatSeakPrefab, child.position, child.rotation)as Rigidbody;
print(clone.velocity);// error occurring here
}
}
}
}
It is crucial to update your code in any case.
It is very heavy on performance.
yup that was the problem, i cast it now as a GameObject and now it works, thanks
Glad it helped. Note the edit I made to the code. It will help your performance.
Your answer
Follow this Question
Related Questions
Gun Script Error (Fixed!!!) 1 Answer
NullReferenceException Error 6 Answers
bullet problem 1 Answer
rigidbody' is not a member of 'UnityEngine.Object 1 Answer
Detect if Transform is destroyed 2 Answers