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 runetimon · Nov 17, 2011 at 01:57 PM · c#declarationeasy

A realy easy c# question

I have started programming c# just for a few days and i learned a bit about variables and that sort of stuff. now i have this script: using UnityEngine; using System.Collections;

 public class AutoAttack : MonoBehaviour {
 
         float maxHealth = 100.0f;
          float curHealth = 100.0f;
         float loadingBarY = 0.9f * Screen.height;
         float loadingBarLength = 50.0f + ((Screen.width * 0.3f) * (maxHealth / curHealth));
          float loadingBarX = (0.5f * Screen.width) - (loadingBarLength / 2.0f);
          float loadingBarWidth = 50.0f;
     
     void Start () 
     {
     }
     
     // Update is called once per frame
     void Update () 
     {
 
     }
     
     void OnGUI()
     {
         GUI.Box(new Rect(loadingBarX,loadingBarY,loadingBarLength,loadingBarWidth), "Hello");    
     }
     
 }

and i have the errors:

Assets/Scripts/AutoAttack.cs(9,76): error CS0236: A field initializer cannot reference the nonstatic field, method, or property AutoAttack.maxHealth' Assets/Scripts/AutoAttack.cs(10,62): error CS0236: A field initializer cannot reference the nonstatic field, method, or property AutoAttack.loadingBarLength'

i'm sorry for being souch a noob, because it will probably be a simple declaration i got wrong but i can't quite seem to find it.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by syclamoth · Nov 17, 2011 at 02:04 PM

Basically, you can't define your initializers in terms of each other. Outside of any function calls, that stuff doesn't actually execute as such- it merely defines the starting state for the rest of the script. If you want to do calculations like that on your initial values, you should do it all in the 'Start' method, which gets called at the very beginning of the game.

 public float maxHealth = 100.0f;
 float curHealth = 100.0f;
 float loadingBarY = 0;
 float loadingBarLength = 0;
 float loadingBarX = 0;
 public float loadingBarWidth = 50.0f;

 void Start () 
 {
     curHealth = maxHealth;
     loadingBarY = 0.9f * Screen.height;
     loadingBarLength = 50.0f + ((Screen.width * 0.3f) * (maxHealth / curHealth));
     loadingBarX = (0.5f * Screen.width) - (loadingBarLength / 2.0f);
 }

Now, since 'curHealth' sounds like the kind of thing that would change at runtime, you should also change loadingBarLength inside of Update so that it always properly reflects how much health your character has.

 void Update()
 {
     loadingBarLength = 50.0f + ((Screen.width * 0.3f) * (maxHealth / curHealth));
     loadingBarX = (0.5f * Screen.width) - (loadingBarLength / 2.0f);
 }

Now, a better question is-

Why is it called 'loading bar' when it's obviously used for current hit points? You should make sure you give your variables accurate names, or it'll confuse the hell out of you in pretty short order.

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 runetimon · Nov 17, 2011 at 02:11 PM 0
Share

Thank you a lot, and why it's named that way is because im just testin and trying to learn, not to actualy make something out of it. but your help is very much appreciated ^^

avatar image Frix · Nov 22, 2012 at 07:55 AM 0
Share

It is good practice to name your variables something relevant too, so start doing that.

avatar image
0

Answer by SARWAN · Nov 22, 2012 at 07:32 AM

In C# you can't define the initialization of variable interms of another variable.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

C# - Simple Voronoi/Delaunay Diagrams For Starters 2 Answers

An OS design issue: File types associated with their appropriate programs 1 Answer

Trying to call a variable from another script in C# 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