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
0
Question by Therian13 · Nov 30, 2013 at 09:01 AM · object-reference-errorexplain

The infamous "NullReferenceException: Object reference not set to an instance of an object" error

I have been getting this error for over 5 days now and still cannot fix it. what am I doing wrong? I have looked at almost every single forum with the same problem and have not been able to fix it. Would someone please take a look at my scripts and tell me what is going wrong?
The full error is " NullReferenceException: Object reference not set to an instance of an object ItemEffect.UseEffect () (at Assets/Inventory/Scripts/Items/ItemEffect.js:35)

Im trying to make it that when I pick up the steak in my inventory and click on it, it activates the function called EatSteak so that it heals my characters hunger by 10 points. But I get the NullReference error when I click on it. What am I missing here? can anyone help me please? I am at my wits end with this one simple error...

Thank you. Below are the two scripts I am using. The first script is called "ItemEffect" and is used for the Steak Item located in my inventory. When I click on the icon, it calls the UseEffect function, which is suppose to call the function from my second script...

 #pragma strict
  
 //This script allows you to insert code when the Item is used (clicked on in the inventory).
  
 var deleteOnUse = true;
  
 private var playersInv : Inventory;
 private var item : Item;
  var other : PlayerStatsV2;
 
 
  
 @script AddComponentMenu ("Inventory/Items/Item Effect")
 @script RequireComponent(Item)
  
 
 //This is where we find the components we need
 function Awake ()
 {
 
 other = gameObject.GetComponent(PlayerStatsV2);
     playersInv = FindObjectOfType(Inventory); //finding the players inv.
     if (playersInv == null)
     {
        Debug.LogWarning("No 'Inventory' found in game. The Item " + transform.name + " has been disabled for pickup (canGet = false).");
     }
     item = GetComponent(Item);
 }
  
 
 //This is called when the object should be used.
 function UseEffect () 
 {
      
         other.EatSteak ();
    
        
        
     //Play a sound
     //playersInv.gameObject.SendMessage("PlayDropItemSound", SendMessageOptions.DontRequireReceiver);
  
     //This will delete the item on use or remove 1 from the stack (if stackable).
     if (deleteOnUse == true)
     {
        DeleteUsedItem();
     }
 }
  
 
 //This takes care of deletion
 function DeleteUsedItem()
 {
     if (item.stack == 1) //Remove item
     {
     
        playersInv.RemoveItem(this.gameObject.transform);
     }
     else //Remove from stack
     {
        item.stack -= 1;
     }
     Debug.Log(item.name + " has been deleted on use");
 }



The second script is called "PlayerStatsV2" I'm trying to call the function EatSteak when I use the script from "ItemEffect", but it gives me the Object reference error.

 #pragma strict
 
 
  var CurrentHP : float = 100.0;
  private var MaximumHP : int = 100.0;
 
  private var CurrentStamina : float = 100.0;
  private var MaximumStamina : float = 100.0;
 
 
 // hunger for survival
 public var CurrentHunger :float = 100.0;
 private var MaximumHunger : int = 100;
 
 // tempeture for survival 
 var CurrentTemp :float = 100.0;
 private var MaximumTemp : int = 100;
 
 //fallrates for survival
 var HungerFallRate : int = 4;
 var TempretureFallRate : int = 3;
 
 
 
   function EatSteak ()
     {
     Debug.Log("this is working, why wont you heal!");
     CurrentHunger += 10.00;
     
     }
 
 }


Thank you for taking the time to read this, and I hope someone can show me what it is I am doing wrong or missing.

Thank you again!

Comment
Add comment · Show 1
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 · Nov 30, 2013 at 09:14 AM 0
Share

Well 'other' is null on line 35 of the first script. Are you sure the component, PlayerStatsV2, exists during awake?

There are one or two things you can try. Either make the 'other' veritable public so it can be assigned in the inspector (and assign i), or try doing an add component ins$$anonymous$$d of a get component (if there isn't anything that has to be configured in the inspector).

Also, can you put in to debug log after the get component in awake to check to see if 'other' is assigned?

1 Reply

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

Answer by fafase · Nov 30, 2013 at 09:05 AM

Try to move this line:

  other = gameObject.GetComponent(PlayerStatsV2);

to the Start. It could be that this component is loaded before the PlayerStatsV2 and then does not find it. Moving it to the Start could fix it.

Comment
Add comment · Show 8 · 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 Therian13 · Dec 01, 2013 at 07:32 AM 0
Share

it is already on the function Awake, so it should be able to find it. I even created a function Start to make sure, but got the same result.

avatar image fafase · Dec 01, 2013 at 08:10 AM 0
Share

Nope, Components are loaded without specific order so it could be that your component looking for PlayerV2 is loaded but PlayerV2 is not yet done. Now if you want to make sure you can drag and drop the script in the variable from the Inspector.

avatar image Therian13 · Dec 01, 2013 at 08:29 AM 0
Share

im not sure I understand. do you mean loading it for the script? or the game? PlayerStats script contains my characters health and other functions, so it starts as soon as the game does. but the other script for using the item should only load up when I pick up the item. or am I misunderstanding you?

avatar image Therian13 · Dec 01, 2013 at 08:52 AM 0
Share

O$$anonymous$$! I found out what is going on. I assign the player to the Other variable, but when I start the game, it takes the player off and changes it back to none as the default. how could I fix this?

I recreated both as prefabs, but it still does it. I also heard of a bug that can make it happen if you have the same script on 2 separate game objects, but so far this is the only object with the item effect script on it.... $$anonymous$$

avatar image fafase · Dec 01, 2013 at 11:11 AM 0
Share

If you assign it manually then remove the line in the Awake.

Show more comments

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

18 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

Related Questions

Multiple Cars not working 1 Answer

Multiple Choice question to popup when pressing a key 0 Answers

I keep getting this error? 1 Answer

I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer

raycasthit questions 2 Answers


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