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 mysticneji · Apr 23, 2013 at 06:48 AM · c#cs0120health bar

Getting error CS0120

Hi, this is my first time asking a question here but it's something that has really stumped me. I'm sort of new to Unity and have been having a hard time finding any tutorials dealing with C# so I've had to convert from Java.

Anyway in the code I've been working on for a health bar I've been getting this error message: Assets/Scripts/GUI Scripts/healthBar.cs(144,35): error CS0120: An object reference is required to access non-static member `healthBar.changeTexture(int)'

Here is the full code: using UnityEngine; using System.Collections; using System.Collections.Generic;

 public class healthBar : MonoBehaviour
 {
     
 PlayerAttributes playerA;
     
 public healthBar SwitchGUI;
     
 healthBar healthObj;
 powerBar powerObj;
     
 public int startHealth = 4;
 public int maxHealth = 8;
 public bool destroyOnDeath = false;
 public bool onHitDie = false;
 private float lastHit = 0.0f;
 public bool dead = false;
     
 public Collider hit;
     
 public List<Texture> switchableTextures = new List<Texture>();
 public int currentTexture = 0;
     
 //public Texture healthTex;
 private Texture texture;
     
 public Texture2D healthTex;
 public Texture2D powerTex;
     
 
 
     // Use this for initialization
     void Start ()
     {
         
         if (switchableTextures.Count > 0) 
         {
             texture = switchableTextures[currentTexture];
         }
         
         playerA = PlayerAttributes.GetInstance();
         if(!healthObj)
         {
             healthObj = GameObject.FindGameObjectWithTag("Player").GetComponent<healthBar>();
         }
         
         playerA = PlayerAttributes.GetInstance();
         if(!powerObj)
         {
             powerObj = GameObject.FindGameObjectWithTag("Player").GetComponent<powerBar>();
         }
         
         //Texture2D healthTex = new Texture2D(128,128);
         //renderer.material.mainTexture = healthTex;
         
         maxHealth = startHealth;
     
     }
     
         
     public void changeTexture(int switchTo) 
     {
         if (switchTo < switchableTextures.Count && switchTo >= 0) 
         {
             texture = switchableTextures[switchTo];
             currentTexture = switchTo;
         } 
 
     }
     
         void up() 
     {
         if ((currentTexture+1) < switchableTextures.Count) 
         {
             ++currentTexture;
             texture = switchableTextures[currentTexture];
         } 
 
     }
     
     void nextTexture() 
     {
         if ((currentTexture+1) < switchableTextures.Count) 
         { // if we are at the end of the array
             ++currentTexture;
             texture = switchableTextures[currentTexture];
         } 
         else 
         {// loop to the beginning
             currentTexture = 0;
             texture = switchableTextures[currentTexture];
         }
     }
     
     void down() 
     {
         if ((currentTexture-1) >= 0) 
         {
             --currentTexture;
             texture = switchableTextures[currentTexture];
         } 
 
     }
     
     
     void StartGame() 
     {
         
         dead = false;
         startHealth = maxHealth;
         
     }
     
     public int currentHealth()
     {
     
         return startHealth;
         
     }
 
 
     void PlayerHit() 
     {
         /*if(onHitDie) 
         {
             Dies();
         }*/
         
         int damage = startHealth-1;
         
         startHealth -= damage;
         startHealth = Mathf.Clamp(startHealth,0,maxHealth);
         float lastHit = Time.time;
     }
     
     // Update is called once per frame
     void Update ()
     {
         if(Input.GetKey("b"))
         {
             healthBar.changeTexture(healthObj.currentHealth);
         }
 
         /*if (startHealth == 0)
         {
             //int startHealth = -1;
             Dies();
         }*/
 
     }
 
     /*void Dies() 
     {
         
         dead = true;
     
         if (destroyOnDeath) 
         {
             Destroy(this.gameObject);
         }
     }*/
     
     void OnGUI()
     {
         //Draw the power bar
         Rect rect = new Rect(0,0,Screen.width/3, Screen.height/10);
         GUI.Button(rect, healthTex);    
         rect.y += rect.height;
         GUI.Button (rect, powerTex);
         rect.y += rect.height;
     }
 
 }

And this is the section that generates the error:

     // Update is called once per frame
     void Update ()
     {
         if(Input.GetKey("b"))
         {
             healthBar.changeTexture(healthObj.currentHealth); // This line
         }
 
         /*if (startHealth == 0)
         {
             //int startHealth = -1;
             Dies();
         }*/
 
     }

I have no idea what is wrong and it's driving me crazy! Any help would be appreciated, thanks!

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
0
Best Answer

Answer by EliteMossy · Apr 23, 2013 at 05:05 PM

healthBar.changeTexture(healthObj.currentHealth); // This line

should just be

changeTexture(healthObj.currentHealth);

You should not be calling the script name. Also for clarity although not strictly a problem, all class names should start with a capital letter and all Methods should start with a capital letter, example:

public class healthBar : MonoBehaviour should be public class HealthBar : MonoBehaviour and public void changeTexture(int switchTo) should be public void ChangeTexture(int switchTo)

http://msdn.microsoft.com/en-us/library/ms229043.aspx

Small edit: you have a few major errors in your code, for example this:

float lastHit = Time.time;

and you have private float lastHit = 0.0f; as a field. This is wrong. I assume you would want to just do lastHit = Time.time;

Another edit: You have even more serious problems.

Now i think i fixed it in the way i see how it should work. If it is not how i think it should work, then the script is completely wrong, and you should rethink it.

 public class HealthBar : MonoBehaviour
 {
 
     PlayerAttributes playerA;
 
     public healthBar SwitchGUI;
 
    // HealthBar healthObj;
     powerBar powerObj;
 
     public int startHealth = 4;
     public int maxHealth = 8;
     public bool destroyOnDeath = false;
     public bool onHitDie = false;
     private float lastHit;
     public bool dead = false;
 
     public Collider hit;
 
     public List<Texture> switchableTextures = new List<Texture>();
     public int currentTexture = 0;
 
     //public Texture healthTex;
     private Texture texture;
 
     public Texture2D healthTex;
     public Texture2D powerTex;
 
 
 
     // Use this for initialization
     void Start()
     {
 
         if (switchableTextures.Count > 0)
         {
             texture = switchableTextures[currentTexture];
         }
 
 
         /*
          * Do you really need this? I mean is this script already attached to the player?
         playerA = PlayerAttributes.GetInstance();
         if (healthObj != null)
         {
             healthObj = GameObject.FindGameObjectWithTag("Player").GetComponent<healthBar>();
         }
         */
 
         playerA = PlayerAttributes.GetInstance();
             if (powerObj != null)
             {
                 powerObj = GameObject.FindGameObjectWithTag("Player").GetComponent<powerBar>();
             }
 
         //Texture2D healthTex = new Texture2D(128,128);
         //renderer.material.mainTexture = healthTex;
 
         maxHealth = startHealth;
 
     }
 
 
     public void ChangeTexture(int switchTo)
     {
         if (switchTo < switchableTextures.Count && switchTo >= 0)
         {
             texture = switchableTextures[switchTo];
             currentTexture = switchTo;
         }
 
     }
 
     void Up()
     {
         if ((currentTexture + 1) < switchableTextures.Count)
         {
             currentTexture++;
             texture = switchableTextures[currentTexture];
         }
 
     }
 
     void NextTexture()
     {
         if ((currentTexture + 1) < switchableTextures.Count)
         { // if we are at the end of the array
             currentTexture++;
             texture = switchableTextures[currentTexture];
         }
         else
         {// loop to the beginning
             currentTexture = 0;
             texture = switchableTextures[currentTexture];
         }
     }
 
     void Down()
     {
         if ((currentTexture - 1) >= 0)
         {
             currentTexture--;
             texture = switchableTextures[currentTexture];
         }
 
     }
 
 
     void StartGame()
     {
 
         dead = false;
         startHealth = maxHealth;
 
     }
 
     public int  CurrentHealth()
     {
 
         return startHealth;
 
     }
 
 
     void PlayerHit()
     {
         /*if(onHitDie) 
         {
           Dies();
         }*/
 
         int damage = startHealth - 1;
 
         startHealth -= damage;
         startHealth = Mathf.Clamp(startHealth, 0, maxHealth);
         lastHit = Time.time;
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKey("b"))
         {
             ChangeTexture(CurrentHealth());
         }
 
         /*if (startHealth == 0)
         {
           //int startHealth = -1;
           Dies();
         }*/
 
     }
 
     /*void Dies() 
     {
  
        dead = true;
  
        if (destroyOnDeath) 
        {
          Destroy(this.gameObject);
        }
     }*/
 
     void OnGUI()
     {
         //Draw the power bar
         Rect rect = new Rect(0, 0, (float)Screen.width / 3, (float)Screen.height / 10);
         GUI.Button(rect, healthTex);
         rect.y += rect.height;
         GUI.Button(rect, powerTex);
         rect.y += rect.height;
     }
 
 }
Comment
Add comment · Show 1 · 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 mysticneji · Apr 23, 2013 at 10:40 PM 0
Share

Thank you for the reply! It works now.

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

12 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to Find Child Of A Child.? C# 1 Answer

Help with more code 2 Answers

Renderer on object disabled after level reload 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