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 Danteeva · Feb 06, 2014 at 09:50 PM · c#nullreferenceexceptionaccessingtostring

NullReferenceException, I don't understand why.

Ok so I'm trying to add attributes that change the characters statistics. I have the character page where the attributes are displayed, I'm trying to change some text so its says how many points are in the attribute but I keep getting the null reference exception error when I have already called it.

     void SetAttributes () {
         Attributes attribs = GetComponent<Attributes> ();
 
         if (isVit == true)
                         GetComponent<TextMesh> ().text = attribs.vitality.ToString ();

vitality is just a public int at the top of a class on another script set to 5.

Comment
Add comment · Show 7
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 robertbu · Feb 06, 2014 at 09:51 PM 0
Share

Is the 'Attributes' component on the same game object as this script? If not, you will get a null reference exception.

avatar image Danteeva · Feb 06, 2014 at 10:02 PM 0
Share

Ahh I see, no its not, is there a way that I don't have to put it on anything, I just want it to store the numbers of the attributes so it can be accessed by other scripts to work out things like the maximum health, and on this script so the player can add more points into them.

avatar image highpockets · Feb 06, 2014 at 10:08 PM 0
Share

Put: Attributes attributes; at the top of your class and initialize it in the start function with: attributes = new Attributes ().

avatar image highpockets · Feb 06, 2014 at 10:23 PM 0
Share

If you are trying to reference it from another object, you should reference the object first and then the component.

attributes = GameObject.Find("ObjectWithAttributes").GetComponent();

avatar image Danteeva · Feb 06, 2014 at 10:40 PM 0
Share

So basically I have to put it on an object then? I suppose the best object would be the character then even though thats in a different scene, or would that not work?

Show more comments

1 Reply

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

Answer by Danteeva · Feb 07, 2014 at 09:51 PM

Right the Dontdestroyonload wasn't working for me, in the end I took out mono behaviour from the attributes script and made the variables static and called the like this

     Attributes.vitality 

very simple, probably why nobody said it :S I got it from this link here if someone else needs it: http://flashpot.wordpress.com/2013/05/16/how-to-pass-data-between-scenes-static-variables/

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

19 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

NullReferenceException error in an array of objects 0 Answers

Can't find source of trigger Null Reference Exception 1 Answer

Accessing RigidbodyFirstPersonController 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