- Home /
"Object Reference not set to an instance of an object"
I'm getting this error, and I can't seem to figure out why... I must be doing SOMETHING wrong, but I just don't know what, Can someone help me out please? (I know what the error means, I just don't know what's wrong) Here is my code:
public void OnGUI() {
pc = (PlayerCharacter)GetComponent("PlayerCharacter");
GUI.skin = InventorySkin;
if (distance < 4 && distance > 1 && pc.EquipedWeapon == ItemDatabase.itemPresets["Old Hatchet"]) { // This is the line that causes the error
GUI.Label( new Rect((Screen.width / 2) - (width/2), (Screen.height / 2) - 140, width, height), "Hold 'E' to chop wood");
}
The error is on line 6, and i believe it has to do with my reference to the "PlayerCharacter" script. I'm trying to check if the EquipedWeapon is a hatchet. EquipedWeapon is a property of the Player Character script. itemPresets is a static dictionary of the ItemDatabase class, which stores all of the items in the game.
Can anyone tell me what's wrong? Thanks
It looks like pc is null. You shouldn't be doing a get component inside a GUI loop either, because its expensive and because it will be executed twice per frame in OnGUI.
$$anonymous$$ake pc a member variable and set it in awake, start or OnEnable, whatever makes most sense for your use case.
Ok thanks I will move it. But you do you have any idea WHY pc is null?
Is PlayerCharacter an object or a component? Did you mean (PlayerCharacter)GameObject.Find("PlayerCharacter"); ?
Don't ever name your question this. Have a look at the hundreds of other questions on the internet about it first. I'm going to go raid hands from the cemetery so I can vote this down harder.
Jessy, I apologize if my question is a problem in your eyes, but believe me, I have looked at many other questions of the same topic. This seemed to be specific to me, and I've been driving myself crazy trying to figure it out myself, so I decided to ask for some help. And, it paid off. It was helpful, so that's all that matters to me.
Anyways, Thanks to amphoterik I figured out what the issue was. Your comment made me realize the script I was looking for was on a different gameobject than the one with this script, so i managed to fix it. And also thanks iwaldrop, I removed the script reference from OnGUI, and i actually noticed a slightly better framerate! Thanks for the help guys!
Answer by Travis-Bulford · Jul 01, 2013 at 03:59 PM
Are you sure pc is the null? there are at least two other items in your line that could be null.
pc.EquipedWeapon
ItemDatabase.itemPresets["Old Hatchet"]
I expect pc.EquipedWeapon might be null.
Add some Debug to your code test each thing if its null it might not be pc and you are misleading yourself.
it was pc, I figured it out. I was trying to access a script from a different gameobject, and forgot to put in the gameObject's reference before GetComponent.
Glad you sorted. I would suggest you try keep that references someplace. Perhaps a singleton. I assume you will be looking it up often.
For example, lets assume you will only even have one object with PlayerCharacter on it.
You could in PlayerCharacter add the following
public static PlayerCharacter instance=null;
public void Start(){
instance=this;
}
// To access player character you only need say
public void OnGUI() {
GUI.skin = InventorySkin;
if (distance < 4 && distance > 1 && PlayerCharacter.instance.EquipedWeapon
== ItemDatabase.itemPresets["Old Hatchet"]) {
GUI.Label( new Rect((Screen.width / 2) - (width/2), (Screen.height / 2)
- 140, width, height), "Hold 'E' to chop wood");
}
You're assumptions are indeed correct, there is only one object with PlayerCharcter attached! I will definitely change it into a singleton. For that, I will accept your answer! Thanks!