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
1
Question by Destr · Mar 07, 2014 at 12:01 AM · mono-behaviour

NULL object have values

Basicly, i think, this problem can be not a unity one, but it is annoying.

I have a class with two fields, inherited from monoBehaviour as usual

  public class Stats : MonoBehaviour
     {
     public int MaxHp;
     public int CurrentHp;
     }

and some other character class with method start and code in it

 void Start()
 {
     this.StatsObject = new Stats {MaxHp = 100, CurrentHp = 99};
 }

Now third script, which needs access to the StatsClass fields... Well,you should better look to the screenshot. I've been developing on C# for some years, but i can't get how this is possible alt text

untitled.png (30.7 kB)
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
2
Best Answer

Answer by robertbu · Mar 07, 2014 at 12:05 AM

You should not use 'new' with any class that you derive from MonoBehaviour. The classes are expected to be a component of a GameObject, so if you really want one from scratch you can do:

 GameObject go = new GameObject();
 this.StatsObject = go.AddComponent<Stats>();

From this limited glimpse, I don't see why you need to derive 'Stats' from MonoBehavior. Given what I see, just remove the ': MonoBehaviour' from the class declaration and continue to use the 'new' operator.

Comment
Add comment · Show 2 · 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 Destr · Mar 07, 2014 at 12:18 AM 0
Share

Ahh, thanks. I've tried and it (almost) workded. But i still cannot understand, how the fields can be initialized, but the class equals to null

avatar image YourGamesBeOver · Mar 07, 2014 at 12:32 AM 0
Share

I would assume that you initialized the fields you added, but Unity needs to initialize the remaining $$anonymous$$onoBehaivor fields. It must do this when it is added to a GameObject.

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

21 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

Related Questions

A node in a childnode? 1 Answer

Issues using the 'new' keyword 3 Answers

GUI problems 0 Answers

Move object Horizontally : 2D 2 Answers

buy older version of unity iPhone for making free games on iPhone 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