Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 rayith · May 29 at 12:27 AM · uicountercount

I created a system that tracks number of matches have

so I have a system that add matches when I pick them and removes them when you light a candle, and when you run out you can no longer light candles. I have a UI script that supposed to display a updated number based on match count. every part works except the unity display

I keep getting this error : NullReferenceException: Object reference not set to an instance of an object FirstPerson.AddMatches (System.Single matches) (at Assets/MyScript/FirstPerson.cs:64) PickupMatch.OnTriggerStay (UnityEngine.Collider target) (at Assets/MyScript/PickupMatch.cs:1

pickupmatch Script:

 void OnTriggerStay(Collider target)
     {
         if (target.tag == "Player")
         {
             if (Input.GetKeyDown(KeyCode.E))
             {
                 txt.text = " ";
                 match.SetActive(false);
                 FirstPerson.AddMatches(5);
             }
         }
     }

FirstPerson Script

  [Header("Matches Parameters")]
     [SerializeField] private static float currentmatches;
     private static UIScript uiscript;
     void Start()
     {
         UIScript uiscript = GetComponent<UIScript>();
     }
     public static void AddMatches(float matches)
     {
         
         currentmatches = currentmatches + matches;
         uiscript.UpdateMatches(currentmatches);
     }
     public static void MinusMatches(float matches)
     {
         currentmatches = currentmatches - matches;
     }
     public static float GetMatches()
     {
         return (currentmatches);
     }

lightacandle

 void OnTriggerStay(Collider target)
     {
         matches = FirstPerson.GetMatches();
 
         if (target.tag == "Player")
         {
             if (Input.GetKeyDown(KeyCode.E) && matches > 0)
             {
                 txt.text = " ";
                 fire1.SetActive(true);
                 FirstPerson.MinusMatches(1);
             }
         }
     }

UI script

 [SerializeField] private TextMeshProUGUI healthText = default;
     [SerializeField] private TextMeshProUGUI staminaText = default;
     [SerializeField] private TextMeshProUGUI matchText = default;
     private float matches;
 
     private void OnEnable()
     {
         FirstPerson.OnDamage += UpdateHealth;
         FirstPerson.OnHeal += UpdateHealth;
         FirstPerson.OnStaminaChange += UpdateStamina;
         UpdateMatches(matches);
     }
 
     private void OnDisable()
     {
         FirstPerson.OnDamage -= UpdateHealth;
         FirstPerson.OnHeal -= UpdateHealth;
         FirstPerson.OnStaminaChange -= UpdateStamina;
     }
 
     private void Start()
     {
         UpdateHealth(100);
         UpdateStamina(100);
         matches = 0;
     }
 
     private void UpdateHealth(float currentHealth)
     {
         healthText.text = currentHealth.ToString("00");
     }
 
     private void UpdateStamina(float currentStamina)
     {
         staminaText.text = currentStamina.ToString("00");
     }
 
     public void UpdateMatches(float currentmatch)
     {
         matchText.text = currentmatch.ToString("00");
     }
 }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by lvskiprof · May 29 at 02:32 AM

A few things I can't verify here...

What is "default" that you are setting healthText, staminaText, and matchText to?

These should not be set in the script, but in the Unity Editor. Have you done that?

In the UIScript, lightacandle, and pickupmatch scripts, where is FirstPerson being set so you can use it (assuming it is supposed to be a reference to the class, as you seem to be using the wrong case for variable and class names here)? Also, where are OnDamage, OnHeal, and OnStaminaChange declared in the FirstPerson script? They appear to be public variable the way you are using there here, but normally they should be changed using a method to set them. For that matter, what are the UpdateDamage, UpdateHealth, and UpdateStamina values you are adding or subtracting on OnEnable() and OnDisable()?

By having the methods and the uiscript variable declared as 'static', every instance of the FirstPerson class shares that variable and those methods, but if there is more than one instance of the UIScript class you will run into trouble. Whatever the last FirstPerson class instance is that runs is what the single uiscript variable will be set to.

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

233 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 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 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 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

counting how many times a specific animation plays 3 Answers

Collision counter 2 Answers

Why Is TextMesh Off by One ??? 0 Answers

How to count instantiated objects that passes a given point (transform.position.y) 1 Answer

count the number on enemyes entrering into a trigger 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