• 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 AbdelrahmanD · Jul 11, 2022 at 01:09 PM · vrbeginnernullreferenceexceptionnull reference exception

NullReferenceException error while trying to retrieve a value from another script


I have a script called Cas$$anonymous$$erReader.cs. T$$anonymous$$s script reads items that are placed in an on-trigger area and keeps updating values in t$$anonymous$$s script such as AppleCount w$$anonymous$$ch is a variable that stores the number of apples placed in the trigger area.

AppleCount is declared as a public variable. I am then trying to access t$$anonymous$$s number from another script called Exit.cs.


T$$anonymous$$s script is as follows:

 public class Exit : MonoBehaviour
 {
     Cas$$anonymous$$erReader record;
     private void Start()
     {
         record = GetComponent<Cas$$anonymous$$erReader>();
     }
     void OnTriggerEnter()
     {            
         int myAppleCount = record.AppleCount;
         Debug.Log("Apple Count = " + myAppleCount);
     }
 }


However,

I keep receiving a NullReferenceException error.

Any idea how I can retrieve t$$anonymous$$s value?


Comment
Add comment · Show 2
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 andrew-lukasik · Jul 11, 2022 at 02:41 PM 0
Share


CashierReader record; means "address to a CashierReader component". It starts null, meaning: it points to nowhere ( yes, null means exactly that and nothing else).


NullReferenceException is raised when you try to get a value of something that is null (no address). If record.AppleCount raises this exception that means that this record points to nothing/`null`.


Since you're setting this reference like this: record = GetComponent<CashierReader>(); - this error means that this gameObject has no CashierReader component attached - simple as that.


avatar image tormentoarmagedoom · Jul 11, 2022 at 03:26 PM 0
Share

Hello! This is a standard answer.

Null Reference errors occurs when there is some variable with value null when code tries to read it. You need to learn to find your problem by your own. First, check your error code, it says the line where the problem is. Second, You need to debug the code while running, and check the states of the variables of the line at the moment the error occurs,

I'm sure you will detect what variable value is NULL. Then investigate why.

Look for some tutorials on how to debug code while running on your scripting software if don't know what I'm talking about.

Bye & good Luck!

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

211 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

Related Questions

Null Reference in UnityStandardAssets.Utility.WaypointProgressTracker.Update 0 Answers

How to make a proper Drag&Drop (any GameObject) for the Hololens (Unity3D) 0 Answers

How do I fix a Null Reference Exception (object reference not set to an instance of an object)? 2 Answers

[Space Shooter Tutorial] NullReferenceException: Object reference not set to an instance of an object PlayerController.FixedUpdate () 1 Answer

GameObject only spawning 60% of the time (c#) 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