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 Xarlexus1 · Jul 28, 2013 at 03:24 AM · transform.positioncompile-errorreference-other-object

ERROR: A field initializer cannot reference the nonstatic field, method, or property `CameraPosition.runner'?

Hello, I am new to scripting and I am trying to create a C# script for the Main Camera so that it will follow a Runner game object by it's X axis position. The Runner has it's own scripts that controls it's movement, while at the same time I do not want the Main Camera to be a child object of Runner so as to prevent it getting disabled from a script. However, when I tried to press play, I get a compiler error that read: "A field initializer cannot reference the nonstatic field, method, or property `CameraPosition.runner'". What does this error mean and what do I need to do? Thank you.

My script:

 using UnityEngine;
 
 public class CameraPosition : MonoBehaviour {
     
     private GameObject runner = GameObject.Find("Runner");
     Vector3 runnerPosition = runner.transform.position;
     
     void Update () {
         transform.localPosition.x = runnerPosition.x;
     }
 }
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
8
Best Answer

Answer by robertbu · Jul 28, 2013 at 03:29 AM

You need to move your initialization inside Start() or Awake():

 using UnityEngine;
  
 public class CameraPosition : MonoBehaviour {
  
     private GameObject runner;
     Vector3 runnerPosition;
 
     void Start() {
         runner = GameObject.Find("Runner");
         runnerPosition = runner.transform.position;
     }
 
     void Update () {
        transform.localPosition.x = runnerPosition.x;
     }
 }
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 roshanjafri31_unity · Mar 17, 2020 at 07:41 PM 1
Share

why do we have to do that ?

avatar image Bunny83 roshanjafri31_unity · Mar 17, 2020 at 08:01 PM 2
Share

Because field initializers are executed before the actual constructor of a class is called. Therefore the object isn't even created / initialized yet. Apart from that the actual construction of the classes is done in Unity's loading thread. That's why you generally shouldn't use code that isn't thread safe in a field initializer or constructor of a class.

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

20 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

Related Questions

2D Game Checkpoint & Respawning 2 Answers

Varying behaviour of transform.position.localPosition.x in StateMachineBehaviour OnStateUpdate 1 Answer

Animate from instantiated GameObject 0 Answers

Does it help performance to first check if some transform attribute has changed before setting it? 1 Answer

transform.position not setting position OR animation setting position even though it shouldn't 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