- Home /
object reference not set to an instance of an object
I am getting a object reference error and I don't know exactly where I am getting it. I know my code could be done way shorter, but I wanted to make it as clear as possible what I am doing. The error is at the following line: if (hit.transform.tag != "Obstacle") {
Piece of script where it came from:
print ("randomDir is 3"); //Go down.
if (Physics.Raycast (transform.position, Vector3.back, 1f)) { //Check for collision with an obstacle.
if (hit.transform.tag != "Obstacle") {
Player.transform.position = Player.transform.position + new Vector3 (0, 0, -1); //Move down by 1..
Steps+= 1;
stepCounter.text = "Steps: " + Steps;
Answer by SohailBukhari · May 12, 2017 at 07:06 AM
you are missing out hit, the out gives you the info about what it hit, like the distance to the hit, the name of the object it hit, the point where it hit, and so on. It can be very helpful in some cases where you wanna know if a laser hit a friendly target or an enemy, for example in your case you want to check tag using hit info.
The out keyword is similar to a pass by ref, where the method that is called is able to modify it. Methods can only return one value, so if you want to pass multiple values out from a method then you can use the out keyword to pass them all out. Out allows for the initial value to be assigned within the calling method whereas a ref would need to have the value originally initialized on the outside.
Call by reference (also referred to as pass by reference) is an evaluation strategy where a function receives an implicit reference to a variable used as argument, rather than a copy of its value(as we use if (Physics.Raycast(transform.position, Vector3.back, out _hit, 1f)) ). This typically means that the function can modify (i.e. assign to) the variable used as argument something that will be seen by its caller. Call by reference can therefore be used to provide an additional channel of communication between the called function and the calling function
if (Physics.Raycast(transform.position, Vector3.back, out _hit, 1f))
{
if (_hit.transform.tag != "Obstacle")
{
Debug.Log("Inside hit");
}
}
so you need to pass out _hit in the parameter and null reference will go away.
Answer by Ahndrakhul · May 11, 2017 at 10:00 PM
Is the "hit" variable a RaycastHit that you have declared somewhere else? You might need to change how you use raycast to:
if (Physics.Raycast (transform.position, Vector3.back, out hit, 1f))
Answer by Patrick2607 · May 11, 2017 at 10:14 PM
You need to instantiate a RaycastHit for the Physics.Raycast method like this:
print ("randomDir is 3"); //Go down.
RaycastHit hit;
//Check for collision with an obstacle.
if (Physics.Raycast (transform.position, Vector3.back, out hit, 1f)) {
Debug.Log("Hit!");
if (hit.transform.tag != "Obstacle") {
//Move down by 1..
Player.transform.position = Player.transform.position + new Vector3 (0, 0, -1);
Steps+= 1;
stepCounter.text = "Steps: " + Steps;
}
}
If the raycast hits something, it will store the object in the RaycastHit variable.
Docs: