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 Exagerate · Jun 04, 2013 at 12:14 AM · javascriptnullreferenceexception

NullReferenceException help! (Linking two javascipt files)

I have two separate scripts, one of them is attached to an edible object, and the other is my health bar. I am having trouble linking the two together (A null reference, probably something to do with an incorrect var?). It doesn't throw the null until the object is eaten, which makes me think i've linked the files together wrongly, or it shouldn't be a boolean (I have no idea what it SHOULD be, if that's the case). So I wonder if someone with better Javascript experience might be able to point me in the right direction. It's all been guesswork so far!

Edible item script (Item.js)

 var eatFood : HealthBar;
 var isFood : boolean = false;
 
 ...
 
 //Player will be able to eat the item (destroy it and add health) if the item is flagged as Food.
 function DoEatFood(){
 
     if(isFood)
 {
     Destroy (gameObject);
     eatFood.isEating = true;
 }
 }

Health bar script (HealthBar.js)

 #pragma strict
 
 var barDisplay : float = 0;
 var health = 1;
 var pos : Vector2 = new Vector2(20,40);
 var size : Vector2 = new Vector2(60,20);
 var progressBarEmpty : Texture2D;
 var progressBarFull : Texture2D;
 
 public var isEating : boolean = false;
 
 function OnGUI()
 {
  
     // draw the background:
     GUI.BeginGroup (new Rect (pos.x, pos.y, size.x, size.y));
         GUI.Box (Rect (0,0, size.x, size.y),progressBarEmpty);
  
         // draw the filled-in part:
         GUI.BeginGroup (new Rect (0, 0, size.x * barDisplay, size.y));
             GUI.Box (Rect (0,0, size.x, size.y),progressBarFull);
         GUI.EndGroup ();
  
     GUI.EndGroup ();
  
 } 
  
 function Update()
 {
     // for this example, the bar display is linked to the current time,
     // however you would set this value based on your desired display
     // eg, the loading progress, the player's health, or whatever.
     barDisplay = health - (Time.time / 1000);
     
     if (barDisplay<=0) {
     Death();
     }
 }
 
 function Death()
 {
     Application.LoadLevel (0);
     Debug.Log("Player Died");
 }
 
 function Eating()
 {
 if(isEating)
     {
     barDisplay +=0.3;
     Debug.Log("Player ate food!");
     }
 }

Thanks community!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by LVBen · Jun 04, 2013 at 02:42 AM

Which var has the null reference?

Comment
Add comment · Show 7 · 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 Exagerate · Jun 04, 2013 at 01:22 PM 0
Share

It points to the "eatFood.isEating = true;" when I click on the red console message.

 NullReferenceException: Object reference not set to an instance of an object
 Item.DoEatFood () (at Assets/Inventory/Item.js:146)
 Item.Update () (at Assets/Inventory/Item.js:83)
avatar image LVBen · Jun 04, 2013 at 06:43 PM 0
Share

It looks like you are destroying the object and then trying to use a var (eatFood) inside of the object. Try changing the order of these two lines of code:

 Destroy (gameObject);
 eatFood.isEating = true;
avatar image Exagerate · Jun 04, 2013 at 07:27 PM 0
Share

That sounded like a good suggestion, I tried it and unfortunately it still gave me:

 NullReferenceException: Object reference not set to an instance of an object
 Item.DoEatFood () (at Assets/Inventory/Item.js:154)
 Item.Update () (at Assets/Inventory/Item.js:88)


It's possibly worth mentioning that it won't let me drag my Health GameObject into this field on the Item.js script:

alt text

screen shot 2013-06-04 at 20.27.07.png (6.1 kB)
avatar image Exagerate · Jun 04, 2013 at 07:29 PM 0
Share

Oh and just incase you are wondering, when it's commented out like this, it works fine and I see the debug when I eat it.

Item.js debug: function DoEatFood(){

     if(isFood)
 {
     //eatFood.isEating = true;
     Destroy (gameObject);
     Debug.Log("Item Eaten");
 }
 }
avatar image LVBen · Jun 04, 2013 at 07:32 PM 0
Share

Where is the code that is calling DoEatFood? One suggestion is to try and narrow down the code that is causing the problem. Try to eli$$anonymous$$ate as much code as you possibly can and get the smallest amount of code possible that still results in the issue. Sometimes if you do that, the problem will be incredibly obvious. Even if it is not totally obvious at that point, then it will make it much simpler to deter$$anonymous$$e the issue.

Show more comments
avatar image
0

Answer by Exagerate · Jun 04, 2013 at 08:33 PM

I've figured out how to do it!

Script1 added a variable:

 var eatFood : HealthBar;

In the first script I needed to call the function in the Start:

 function Start() {
 
 Debug.Log("Healthbar Loaded!");
 eatFood = new HealthBar();
 
 }

And then:

 function DoEatFood(){
  
 if(isFood)
 {
     Destroy (gameObject);
     GameObject.Find("Health").GetComponent(HealthBar).Eating();
 }
 }


In the second script I didn't need any variable, just the function:

 function Eating()
 {
     health +=1;
     Debug.Log("Player ate food!");
 }



Thanks for your help!

Comment
Add comment · 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

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

15 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

Related Questions

"NullReferenceException" Error when trying to count objects with a particular tag using GameObject.FindGameObjectsWithTag function 1 Answer

NullReferenceException: Object reference not set to an instance of an object 3 Answers

Help with eval() 0 Answers

NullReferenceException: Object reference not set to an instance of an object 2 Answers

Why can't I access the TextMesh component of a GameObject? 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