Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by pickle chips · Jul 01, 2013 at 04:01 AM · errorreferenceinstance

"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

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iwaldrop · Jul 01, 2013 at 04:11 AM 0
Share

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.

avatar image pickle chips · Jul 01, 2013 at 01:22 PM 0
Share

Ok thanks I will move it. But you do you have any idea WHY pc is null?

avatar image amphoterik · Jul 01, 2013 at 01:28 PM 0
Share

Is PlayerCharacter an object or a component? Did you mean (PlayerCharacter)GameObject.Find("PlayerCharacter"); ?

avatar image Jessy · Jul 01, 2013 at 02:15 PM 1
Share

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.

avatar image pickle chips · Jul 01, 2013 at 03:46 PM 0
Share

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!

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pickle chips · Jul 01, 2013 at 04:09 PM 0
Share

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.

avatar image Travis-Bulford · Jul 01, 2013 at 04:15 PM 0
Share

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");
     }
avatar image pickle chips · Jul 02, 2013 at 01:37 AM 0
Share

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!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Script decides not to run anymore 1 Answer

NullReferenceException Error 2 Answers

Reference script on Behaviour is Missing, but IT'S NOT MISSING 0 Answers

Getting UnassignedReferenceException even though reference assigned 1 Answer

Really need help with: Ambiguous reference 'constructor': UnityEngine.GUIContent.constructor(UnityEngine) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges