- Home /
NullReferenceException: Object reference not set to an instance of an object
Hello! Firstly I thank you in advance because I am really stuck. I do not usually ask questions because usually I can find the solution by searching the internet.
Here is the error I get and this is my script. NullReferenceException: Object reference not set to an instance of an object Gun.Update () (at Assets/Gun.cs:43)
using UnityEngine;
using System.Collections;
public class Gun : MonoBehaviour {
Vector3 position = Vector3.zero;
public Rigidbody2D projectile;
private Vector3 positionmouse = Vector3.zero;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if ((Input.touchCount > 0 &&
Input.GetTouch(0).phase == TouchPhase.Began))
{
position.x = transform.position.x;
position.y = transform.position.y;
position.z = 0;
Vector3 positiontouch = new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 0);
Vector3 look = Camera.main.ScreenToWorldPoint(positiontouch);
Rigidbody2D go = Instantiate (projectile, position, Quaternion.Euler(0,0,0)) as Rigidbody2D;
go.transform.LookAt(look);
go.AddForce(go.transform.forward * 1100);
}
if((Input.GetButtonDown("Fire1")))
{
position.x = transform.position.x;
position.y = transform.position.y;
position.z = 0;
positionmouse.x = Input.mousePosition.x;
positionmouse.y = Input.mousePosition.y;
Vector3 look = Camera.main.ScreenToWorldPoint(positionmouse);
Rigidbody2D go = Instantiate (projectile, position, Quaternion.Euler(0,0,0)) as Rigidbody2D;
go.transform.LookAt(look);
go.AddForce(go.transform.forward * 1100);
}
}
}
I modified my code many times to find the solution, but to no avail. The code above is the latest version, I have not been remodified.
Thank you in advance!
Why slice up Input.mousePosition only to rebuild it as a parameter pass for Camera.main..? Do
Vector3 look = Camera.main.ScreenToWorldPoint(Input.mousePosition);
I haven't used Instantiate with a cast to Rigidbody2D but it seems ok; did 'go.transform' autocomplete in $$anonymous$$ono? $$anonymous$$aybe try go.gameObject.transform
Answer by FuzzyLogic · Feb 17, 2014 at 06:37 AM
You are mixing up the RigidBody2D and the GameObject references.
Replace line 43 to 45 with the following lines:
if (go == null) return; // oops, no object instantiated! bail out
go.gameObject.transform.LookAt(look);
go.AddForce(go.gameObject.transform.forward * 1100);
Hi FuzzyLogic! I tried to enter your code. In fact there is no more crash, the projectile is instantiated but the "lookat" and "addforce" do not work.
Those lines will never be executed if 'go' is null. Test that the code is getting past the null check by adding this line just before the LookAt line:
Debug.Log("'go' is not NULL");
You should see that message flooding your Console window when you shoot. If you don't, then there is something wrong with your Instantiate.
I fixed part of the problem, but I am confronted with another. I think the prefab was corrupted because I have deleted and recreated and this time the instance of the projectile was not null. However, the other problem is that the instance that is created moves very slowly and does not have the right rotation I took.
How can i fix this please ? :/
If go is null, then you should check that projectile is not null before calling Instantiate.
if (projectile == null) return; // oops! bail out
Debug.Log("'projectile' is not NULL");
Rigidbody2D go = Instantiate (projectile, position, Quaternion.Euler(0,0,0)) as Rigidbody2D;
Your instanced projectile is Looking at the mouse cursor. Did you mean to align it to always face the camera?
Answer by mattyman174 · Feb 16, 2014 at 10:55 PM
public Rigidbody2D projectile;
Rigidbody2D go = Instantiate (projectile, position, Quaternion.Euler(0,0,0)) as Rigidbody2D;
Do you have a Rigidbody assigned to this Public Variable before playing the Script?
Otherwise your trying to Instantiate a Null Object.
Answer by Nighty22Night · Feb 17, 2014 at 06:24 AM
Thank you for your very quick answers! From your advice, I changed the code like this:
if(look != null)
{
go.gameObject.transform.LookAt(look);
go.rigidbody2D.AddForce(go.transform.forward * 1100);
}
I still have the same error... Projectile is indeed assigned to a rigidbody, and besides I shoot in the game, you can see the projectile appear before the crash the game.
The bomber is the "projectile".
So, How can I fix this? Again thank you for your answers
This is not an answer. You can actually EDIT your question.
Your answer

Follow this Question
Related Questions
Upon clicking: Object reference not set to an instance of an object 0 Answers
Bug in Object reference ? 0 Answers
nullreference exeptio, object reference not set to an instance of an object 1 Answer
An object reference that seems pretty set-to-an-instance-of-an-object to me 0 Answers
NullReference on textfield 1 Answer