- Home /
Trouble with null object reference.
I've been trying to get some red paint to fire from a gun in a 2D, side-scrolling game, but I've been having trouble with the actual movement of the paint. As soon as I instantiate the paint at the guns barrel, it sits still despite my AddForce behavior, and apparently it's due to the FirePaint() method having its argument "GameObject p" (which will be used later in the game to detect which color of paint is equipped) it throws this error:
Object reference not set to an instance of an object PaintFiring.FirePaint (UnityEngine.GameObject p) (at Assets/Scripts/PaintFiring.cs:27) PaintFiring.Update () (at Assets/Scripts/PaintFiring.cs:20)
Here's my code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PaintFiring : MonoBehaviour {
public GameObject redPaint;
public Transform barrelEnd;
public int paintSpeed = 5;
void Start()
{
redPaint = GameObject.Find ("RedPaint");
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
FirePaint(redPaint);
}
}
void FirePaint(GameObject p)
{
Rigidbody2D paintClone;
paintClone = Instantiate(redPaint, barrelEnd.position, barrelEnd.rotation)as Rigidbody2D;
paintClone.rigidbody2D.AddForce(new Vector2(paintSpeed, 0));
}
}
I have no idea why this would throw the exception it did. I'm passing a parameter GameObject in the Update method, which is what Unity says I'm not passing. I'm probably missing something basic, so please excuse me in that regard. I'm still new to C#, and i'm not completely up to date on the differences between UnityScript and C#. I have used it outside of Unity, but I don't know how to use it in Unity very well yet...
Have you checked for typos: is 'RedPaint' actually 'redPaint'?
Answer by Eric5h5 · Oct 14, 2014 at 01:05 AM
You defined redPaint as a GameObject but you're attempting to instantiate it as a Rigidbody2D. You can't cast a GameObject to a Rigidbody2D so it's just null.
Answer by JanusMirith · Oct 14, 2014 at 03:30 AM
//Put this into your code to see if this is where you messed up
//Spelling and caps matter, always make sure you have found a object after looking, unity will
// not through an error if it can not find the object it just returns null
void Start()
{
redPaint = GameObject.Find ("RedPaint");
//Look to see if we have found the gameobject
if (redPaint == null){
Debug.LogError("Can not find RedPaint");
}
}
I used both solutions. In good news, the paint is now flying like it should, but I'm still having the null prefab exception thrown at me everytime I play. I have to manually set the Red Paint in the prefab, and in reply to richyrich, I have checked, the code is set to look for "Red Paint", the GameObject of Red Paint is "Red Paint", but the prefab is "RedPaint". Although when I swapped it over to contain the prefab name, it still throws the error.
When I used the extra debug to check if "redPaint" was null, it returned null, but I have no idea why it would.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to convert to 2D 1 Answer
2D PlatformerController: Cumulative drift? 1 Answer
Replacement for LookAt in 2D 1 Answer
2d Game Jump Issue 2 Answers