Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by nickilak · Jun 12, 2016 at 09:07 PM · errorobjectpickup

Object Reference Error

I'm creating pickups that will modify my HUD text once they are picked up. Instead, they are just giving me object reference errors. Any tips would be appreciated!

     public void ModHealth(int _value)
     {
         CurHealth += _value;
         PlayerHUD.updateHealth(CurHealth);
     }

This is one of the examples of it going wrong. I have it Curhealth setup with a int as well as having a PlayerHUD reference. What could be the issue whenever I pick up an item?

Comment
Add comment · Show 9
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 TBruce · Jun 12, 2016 at 10:05 PM 0
Share

That little blurb of code is not enough so that anyone can help you.

One needs at a $$anonymous$$imum the script with the code you are showing and the PlayerHUD script could help as well.

avatar image nickilak TBruce · Jun 12, 2016 at 10:10 PM 0
Share

Sorry! The scripts that they are contained in are pretty long so I didn't know if they would be able to be pasted. Let me try and make an imgur of them.

avatar image TBruce nickilak · Jun 12, 2016 at 10:16 PM 0
Share

Try just the first one.

avatar image nickilak TBruce · Jun 12, 2016 at 10:20 PM 0
Share

Here is the code that is related to my player script.

[SerializeField] int $$anonymous$$axLives = 3; private int CurLives = 3;

 //Player: Stats: $$anonymous$$ax & Current Ammo
 [SerializeField]
 int $$anonymous$$axAmmo = 50;
 private int CurAmmo = 50;

 //Player: Stats: $$anonymous$$ax & Current Shields
 [SerializeField]
 int $$anonymous$$axShields = 100;
 private int CurShields = 100;

 //Player: Stats: $$anonymous$$ax & Current Health
 [SerializeField]
 int $$anonymous$$axHealth = 3;
 private int CurHealth = 3;


 void OnTriggerEnter(Collider other)
 {
     if (other.tag == "PickUp")
     {
         if (other.name == "Score")
         {
             $$anonymous$$odScore(5);
             Destroy(other.gameObject);
         }
         if (other.name == "Life")
         {
             $$anonymous$$odLives(1);
             Destroy(other.gameObject);
         }
         if (other.name == "Health")
         {
             $$anonymous$$odHealth(1);
             Destroy(other.gameObject);
         }
         if (other.name == "Shield")
         {
             $$anonymous$$odShields(25);
             Destroy(other.gameObject);
         }
         if (other.name == "Ammo")
         {
             $$anonymous$$odAmmo(10);
             Destroy(other.gameObject);
         }
         if (other.name == "Negative")
         {
             $$anonymous$$odShields(25);
             Destroy(other.gameObject);
         }
         else if (other.tag == "Basic")
         {
             $$anonymous$$odShields(-10);
         }
         else if(other.tag == "Fighter")
         {
             $$anonymous$$odHealth(-1);
         }
         else if(other.tag == "Strong")
         {
             $$anonymous$$odLives(-1);
         }
     }
 }

 //HUD: Player: Updates Ammo
 public void $$anonymous$$odAmmo(int _value)
 {
     CurAmmo += _value;
     PlayerHUD.updateAmmo(CurAmmo);
 }

 //HUD: Player: Updates Health
 public void $$anonymous$$odHealth(int _value)
 {
     CurHealth += _value;
     PlayerHUD.updateHealth(CurHealth);
 }

 //HUD: Player: Updates Shields
 public void $$anonymous$$odShields(int _value)
 {
     CurShields += _value;
     PlayerHUD.updateShield(CurShields);
 }

 public void $$anonymous$$odScore(int _value)
 {
     Score += _value;
     PlayerHUD.updateScore(Score);
 }

 public void $$anonymous$$odLives(int _value)
 {
     CurLives += _value;
     PlayerHUD.updateLives(CurLives);
 }




avatar image TBruce nickilak · Jun 12, 2016 at 10:29 PM 0
Share

Because PlayerHUD is not declared I must assume that it is static.

If that is the case then the problem is in the PlayerHUD script which I need to see.

Otherwise it needs to be declared somehow. But what is important is where is the script attached relative to the player script. It may as simple as addiny a property in the player script above and set it in the inspector.

Show more comments
avatar image TBruce · Jun 12, 2016 at 11:22 PM 0
Share

I need to see the whole class, not just the contents (that goes for each class)

In other words I need to see all this (for example)

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$yClass : $$anonymous$$onoBehaviour
 {
 // yada yada yada
 }
avatar image nickilak TBruce · Jun 12, 2016 at 11:52 PM 0
Share

$$anonymous$$y apologies, this is the everything for the HUD

  using UnityEngine;
  using System.Collections;
  using System.Collections.Generic;
 
     public class HUD : $$anonymous$$onoBehaviour {
 
    
      public Text$$anonymous$$esh scoreText$$anonymous$$esh;
      private string scoreText;
      public Text$$anonymous$$esh ammoText$$anonymous$$esh;
      private string ammoText;
      public Text$$anonymous$$esh shieldText$$anonymous$$esh;
      private string shieldText;
  
  
      [SerializeField]
      public int $$anonymous$$axAmmo = 50;
      [SerializeField]
      public int Ammo = 10;
  
      public LifeIcon lifeIconObj;
      private List<LifeIcon> lifeIconInstances;
      public HealthIcon healthIconObj;
      private List<HealthIcon> healthIconInstances;
  
      // Use this for initialization
      void Start () {
          scoreText = scoreText$$anonymous$$esh.text;
          //HUD: Ammo Count: Percent
          updateAmmo(Ammo = Ammo / $$anonymous$$axAmmo);
          CreateLifeIcons(3);
          CreateHealthIcons(3);
      }
  
      //HUD:Ammo Count
      public void updateAmmo(int value)
      {
          ammoText$$anonymous$$esh.text = ammoText;
          ammoText$$anonymous$$esh.text += value;
      }
  
      public void updateScore(int value)
      {
          scoreText$$anonymous$$esh.text = scoreText;
          scoreText$$anonymous$$esh.text += value;
      }
  
      //HUD:Shield Strength
      public void updateShield(int value)
      {
          shieldText$$anonymous$$esh.text = shieldText;
          shieldText$$anonymous$$esh.text += value;
      }
  
      public void updateLives(int numLivesRemaining)
      {
          for (int i = 0; i < lifeIconInstances.Count; ++i)
          {
              bool bActive = i < numLivesRemaining;
              lifeIconInstances[i].gameObject.SetActive(bActive);
          }
      }
  
      public void updateHealth(int numHealthRemaining)
      {
          for (int i = 0; i < healthIconInstances.Count; ++i)
          {
              bool bActive = i < numHealthRemaining;
              healthIconInstances[i].gameObject.SetActive(bActive);
          }
      }







avatar image nickilak nickilak · Jun 12, 2016 at 11:53 PM 0
Share

and this is just the class for the player(first code I posted)

 using UnityEngine;
 using System.Collections;
 
 public class PlayerShipController : $$anonymous$$onoBehaviour
 {

1 Reply

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

Answer by TBruce · Jun 13, 2016 at 12:12 AM

So the problem seems to be that you are not declaring a reference to HUD like so

 public HUD PlayerHUD; // this is set in the inspector

 void Start()
 {
     if (PlayerHUD == null)
     {
         Debug.LogError("The PlayerHUD property has not been set in the inspector. Please set now and try again.");
     }
 }

or like this

 private HUD PlayerHUD;
 
 void Start()
 {
     if (GetComponent<HUD>() != null)
     {
         PlayerHUD = GetComponent<HUD>();
     }
     else
     {
         Debug.LogError("Missing PlayerHUD component. Please attach one and try again.");
     }
 }
Comment
Add comment · Show 1 · 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 nickilak · Jun 13, 2016 at 12:42 AM 0
Share

I'm absolutely silly, I never set my HUD inside my inspector when I opened my new scene. Thank you for re$$anonymous$$ding me with the debug LOL.

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

68 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Pick up, Carry, Throw Object - Mobile Controls 0 Answers

Object reference not set to an instance of an object 1 Answer

How do I get the name of the object I am attacking and how can I use it to select the game object name? 0 Answers

how do i pick up objects 1 Answer

How do I allow 3rd Person Character Picking Up Objects within a range? 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