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 maxnorth · Mar 12, 2015 at 02:21 PM · c#errorthis

Why am I getting this error: "Keyword 'this' is not available in current context."

 using UnityEngine;
 using System.Collections;
 
 public class PlayerLife : MonoBehaviour 
 {
       public LifeState lifeState = new LifeState(this);
 
       public class LifeState
       {
         private PlayerLife playerLife;
         private bool currentState;
         public bool alive
         {
             get {return currentState;} 
             set 
             {
                 if (value != currentState)
                 {
                     currentState = value;
                     if (value) playerLife.SpawnPlayer();
                     else playerLife.KillPlayer();
                 }
             }
         }
 
         public LifeState(PlayerLife container) 
         {
             playerLife = container;
             currentState = false;
         }
     }
 
     void SpawnPlayer() {}
     void KillPlayer () {}

}

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 tigertrussell · Mar 12, 2015 at 02:35 PM

You need to do that in an Awake() or Start() method; not where you are making your properties declarations.

 public class PlayerLife : MonoBehaviour 
 {
        public LifeState lifeState;
 
        void Awake() {
             lifeState = new LifeState(this);
        }
  
        public class LifeState {...}

because the this keyword is relative to an instance of the class, not a property of the class itself.

Comment
Add comment · Show 3 · 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 maxnorth · Mar 12, 2015 at 02:43 PM 0
Share

You're right, that worked. I guess I still don't entirely understand why, though. Aren't the initializers called when the class is instantiated? Or are only constructors called upon instantiation, and field initializers have to be statically compiled?

avatar image maxnorth · Mar 12, 2015 at 02:51 PM 0
Share

I found a useful page on this issue saying pretty much the same thing. Apparently it's also not allowed to initialize a variable by referencing a previously initialized field of the same class either. Didn't know that.

https://msdn.microsoft.com/en-us/library/aa645759%28v=vs.71%29.aspx

avatar image tigertrussell · Mar 12, 2015 at 04:10 PM 0
Share

The "real" reason is that under the hood the compiler is trying to get a generic description of each class for purposes of allocating memory and understanding what to expect from other code later on down the line. When the compiler looks at your classes, the first thing it checks are the properties and the signatures of the functions to get this generic "description" of the class. At that time, you can choose to initialize a "default" value, but only if it is constant (i.e. the compiler can easily initialize it). At that point in time, things like the this keyword are meaningless to the compiler, and there is no context under which you can refer to pre-existing code (because it hasn't compiled it yet, it's just trying to get a picture of the class).

There's some mild oversimplification and psuedoscience in that explanation, but that's the gist of it. $$anonymous$$aybe someone else in the community who knows a bit more about the lower-level stuff will chime in?

edit: oh so I guess the answer to your question is "yes" field initializers are statically compiled; constructors are called upon initialization.

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

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

No Monobehaviour scripts in files 1 Answer

2-Dimensional Array Error [CLOSED] 1 Answer

Help I am getting an error CS0079 in my code and I don't know why. 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