- Home /
this.gameObject null reference exception
Hello, I am having an issue with this.gameObject being null. I am unsure as to why it is saying that it does not exist. It isn't an instantiated object. It is there in the editor. There aren't any Destroy calls made on these objects.
Here is some of the code:
public void UpdateCamera(){
switch(controllingPlayer){
case playerSlot.AI:
break;
case playerSlot.One:
gameCameraScript.SetPlayer(1,this.gameObject);
break;
case playerSlot.Two:
gameCameraScript.SetPlayer(2,this.gameObject);
break;
case playerSlot.Three:
gameCameraScript.SetPlayer(3,this.gameObject);
break;
case playerSlot.Four:
gameCameraScript.SetPlayer(4,this.gameObject);
break;
default:
Debug.Log("Something broke in camera.");
break;
}
}
The error occurs for any of the "this.gameObject" references.
and,
This is the SetPlayer method being called here:
public void SetPlayer(int _playerNumber,GameObject _playerObject){
switch(_playerNumber){
case 1:
playerOne = _playerObject;
break;
case 2:
playerTwo = _playerObject;
break;
case 3:
playerThree = _playerObject;
break;
case 4:
playerFour = _playerObject;
break;
default:
break;
}
}
Does anyone know why this may be happening now? And hasn't since 6/26.
In case it helps the error is:
NullReferenceException: Object reference not set to an instance of an object
Answer by StewVanB · Jul 08, 2013 at 08:00 PM
Ok, so i went to lunch and when I returned this is no longer an issue. It just "works" again. No code changes, perhaps this is a bug in this version of unity?
Sorry about all the posts on this topic. When I got started working on my project again this morning, the issue has started once again. This is really putting a damper on my productivity. Should I contact unity support about this issue?
Yes; send them your project with the built-in bug reporter.
Answer by Filippo94 · Jul 09, 2013 at 02:07 PM
Can't you just use gameObject?
I would think so, however all of these return null:
gameObject, this.gameObject, transform.gameObject, this.transform.gameObject, etc.basically all references to gameObject
Answer by cerebrateB · Aug 20, 2015 at 09:41 PM
Okay, So I know that this question is years old, but I'm leaving my answer here just in case someone else stumbles across the same issue like I did.
Debug.Log("This gameObject is: " + this.gameObject); // <= this works
ship.SendMessage("AddShipComponent", this.gameObject); // <= this doesn't
I kept getting null reference errors on the line with the send message, despite the debug log line printing out what I would expect it to. After a couple days of pulling my hair out, I finally moved the script in question to execute after default time in the scrip execution order. After doing so, everything worked as expected.
I know this is very old, but the likely reason that script execution order fixed it for you is because ship was null. Changing the execution order caused the ship to get initialized early enough that you no longer see the error. So its not really an answer to op
Your answer
Follow this Question
Related Questions
NullReferenceException while trying to access script on object 2 Answers
Using Scripts in AssetBundles 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
NullReference when accessing GameObject in array (C#) 1 Answer
How to form copies of yourself that follow you when you walk? (2D Platformer) 2 Answers