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 Topthink · Aug 14, 2017 at 04:09 PM · variablesnull reference exceptionsyntax

How do I resolve NullReferenceException ??

I'm trying to access a public variable from another script and I'm getting a NullreferenceException.

I've searched for and read many of the 150+ pages on the issue and I think I can get around it with a "FInd" but I'm having a hard time with the syntax...I can't find any examples that are on point with the syntax I need.

Here's the Variables I want to access:

 public class GameVars : MonoBehaviour 
 {
     public int RedStrength;
     public int BlueStrength;
 }

BUT...when I try to access them from another script I get the null reference error.

Here is one example of the "Find" I've tried in another script...and I've tried different combination/variations of syntax from LOTS of "answers" but none seem to be what I need.

     void Awake()
     {
         GameObject go = GameObject.Find ("BlueStrength");
         go.GetComponent<GameVars> ().BlueStrength;
     }

ODDLY...I was able to access this variable from the other script for awhile (without using any sort of "find") but then I must have changed something that triggered the Null Exception error and now even though I've tried to replicate the problem free code I still get the error.

Thanks in advance for any help.

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 Topthink · Aug 14, 2017 at 04:28 PM 0
Share

Funny thing...I'm continuing to do searches on this matter to attempt to find a solution...I finally found one in a search that, at last, seemed exactly on point and I opened it with great hope of finding a resolution...

...only to find that it was my very own question I just posted a few $$anonymous$$utes earlier.

Okay, well, I guess you had to be there. :-)

1 Reply

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

Answer by Cuttlas-U · Aug 14, 2017 at 04:47 PM

hi;

try to find it by the type of it and not with name like this :

 GameObject.FindObjectOfType<GameVars>().BlueStrength; 



======================

so what are u doing in this lane ?

 go.GetComponent<GameVars> ().BlueStrength;

u are just getting an access to an int but not doing any thing or assign it to another int ;

Comment
Add comment · Show 4 · 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 Topthink · Aug 14, 2017 at 05:01 PM 0
Share

Thanks. I was trying to assign it values at the same time or immediately after I tried to find it but I was still getting the NullException error. In fact, I assigned it values in the script "GameVars" and then tried to access/change those values but I still get the NullException error.

I was trying to FIRST "find" it somehow so I could access/assign/change its value but I can't figure out the syntax to "find" it.

avatar image Topthink · Aug 14, 2017 at 05:44 PM 0
Share

I'm still getting errors...I'm not figuring out exactly what I need to do and even if I knew whether to use the "Find" or "FindObjectOfType" I'm still not sure of the syntax and I can't seem to find it anywhere.

avatar image Topthink · Aug 14, 2017 at 09:40 PM 0
Share

Still not working. I'm still looking up stuff and trying different things off an on.

avatar image Topthink · Aug 14, 2017 at 10:10 PM 0
Share

I figured it out based on what you wrote above. At first, I didn't fully understand it. But after I looked more thoroughly, I finally got what you were saying.

Thanks.

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

111 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

Related Questions

Use enum hidden value as something other than int? (C#) 2 Answers

Variables not showing in Inspector 2 Answers

Why is this not working? 0 Answers

Why is Unity showing the wrong variables in the Inspector? 1 Answer

Access To a Variable of another script which in another scene 0 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