NullReferenceException...cant find any NullReference!

Hi, can anybody please tell me why I have a NullReferenceException in ln91, column 64 ? I dont find any mistake in my script! Please help!

function DestroyItem(){
Ln91 if((transform.position - gameObject.Find(“InventoryCube”).transform.position).magnitude < minCubeDist){
Destroy(gameObject.Find(“InventoryCube”));
ItemtoInventory.CugotoInventory = true;
}
if((transform.position - gameObject.Find(“InventorySphere”).transform.position).magnitude < minSphereDist){
Destroy(gameObject.Find(“InventorySphere”));
SpheretoInventary.spgotoInventary = true;
}
}

My first thought would be, it does not seem like you are checking if gameObject.Find is really returning a true reference or just a null object. Perhaps you might want to throw in a if statement to see what the Find method is returning and then go from there.

gameObject refers to the current game object.
GameObject refers to the class with a Find method in it.

My bet is that gameObject.Find(“InventoryCube”) returns null so accessing .transform of null results in a null reference exception.

Sidenote:

  1. GameObject.Find is really slow so save the result of find in a variable.
var obj = GameObject.Find("InventoryCube");
if((transform.position - obj.transform.position).magnitude < minCubeDist)
{
   Destroy(obj);
}
  1. Use code tags to post code

hmmm… it still dont helps , if I put those things in variables…I only get a NullReference at line 91, where the Cube is mentioned, but not where “InventorySphere” is mentioned…strange…

In addition to what the others have said above…

If the code you posted is a direct cut/paste from your actual source, there’s a problem here:

gameObject.Find("InventoryCube").transform.positio n)

Notice the space before the “n” in “position”. It’s much easier to see when displayed within CODE tags due to the monospaced font.

Edit: There’s a similar problem here:

gameObject.Find("InventorySphere").transform.posit ion)

Jeff

oh…now I know what the mistake is: The mistake is , that “The Object of Type GameObject has been destroyed, but I am still trying to accesss it”…I dont know how to solve that so please help!

I believe that is a problem with the forum, not his code. I have had multiple times when I posted transform.position and had such spaces occur when they were not in my code.

You seem to be destroying the gameobjects every time the conditions are met. Are you sure that there is always a GameObject with that name? Otherwise you need to put a few checks in there.

sorry

Yes you are right, I see my mistake now…anyways Im cleaning up my work and restarting, because I have a better vision now, how to do my work more efficiently…but I have one question : How can I disable player movement if a player has been colliding with a trigger?
I created 2 boolean variables, one for vertical direction and one for horizontal direction(using inout.getaxis())…I want the player to not be able if he enteres the trigger(a cube) and only after that be able to move again if he presses “m”…how can I do that?
Thanks!

i mean Input.GetAxis…

Create a boolean variable for example of canMove and have your update loop check against that value before processing input. If true, go ahead and do any input updates, otherwise skip it.

Yes, I am actually doing it like that, but still the player can proceed walking…i check the state of a boolean variable “canMove” in Update() function,before my PlayerTransform() Funtion can be called, it is not supposed to be called, if “canMove” = false and it is right then false at the point the player enters the trigger, else it is true…but still the player can move on…i only notice a short hasitation the first time the player enters the trigger…

You should probably post the relevant code…