- Home /
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!
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;
}
}
Your answer
Follow this Question
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