- Home /
Can't access variable from another script
When a player catches boost(additional life) I want to access my health script and add +1 to the currentHealth. It doesn't work. Here my boost life script:
public GameObject particles;
public bool isOnTrigger = false;
void OnTriggerEnter (Collider other) {
if (other.tag == "Player") {
GameObject.FindWithTag("Player").GetComponent<PlayerHealth>().currentHealth+=1;
GameObject part = Instantiate(particles,transform.position,Quaternion.identity)as GameObject;
Destroy(part,2);
Destroy (this.gameObject);
}
}
}
This is HealthScript attached to the player:
public int currentHealth;
private int startHealth = 7;
private GUIStyle styleForHealth;
private GUIStyle healthBarBG;
private float healthBarLength;
private bool showHealthBar = true;
void Start(){
currentHealth = startHealth;
}
void Update () {
var scoreScr = Camera.main.GetComponent<ScoreScript> ();
currentHealth = startHealth - scoreScr.miss - scoreScr.crush;
if (currentHealth > startHealth)
currentHealth = startHealth;
if (currentHealth < 0)
currentHealth = 0;
if (currentHealth<1) {
GameObject.FindWithTag("Player").GetComponent<PlayerController>().canControl = false;
Invoke ("LevelStart", 2);
}
healthBarLength = ((Screen.width / 4 ) /startHealth)*currentHealth;
if (healthBarLength < 1) {
showHealthBar = false;
healthBarLength = 0;
}
}
void LevelStart(){
Application.LoadLevel (2);
}
void OnGUI(){
InitStyles();
GUI.Box (new Rect(Screen.width/2+195,10,Screen.width/4+10,30)," ",healthBarBG);
if(showHealthBar)
GUI.Box (new Rect(Screen.width/2+200,15,healthBarLength,20),currentHealth+"/"+startHealth, styleForHealth);
}
private void InitStyles(){
if( styleForHealth == null ){
styleForHealth = new GUIStyle( GUI.skin.box );
styleForHealth.normal.background = MakeTex( 2, 2, new Color( 0.4f, 0.8f, 0f, 1f ) );
}
if (healthBarBG == null) {
healthBarBG = new GUIStyle(GUI.skin.box);
healthBarBG.normal.background = MakeTex(2,2, new Color(0f, 0.4f, 0f, 1f) );
}
}
private Texture2D MakeTex( int width, int height, Color col ){
Color[] pix = new Color[width * height];
for( int i = 0; i < pix.Length; ++i ){
pix[ i ] = col;
}
Texture2D result = new Texture2D( width, height );
result.SetPixels( pix );
result.Apply();
return result;
}
Thank you in advance.
Can you specify the phrase "It isn't working."
Do you have any error messages in the console that you can post? Is something else happening other than "It isn't working."? :-)
No, I don't have any error messages, but the variable currentHealth is not changing.
And I assume all the other stuff happens (the destroys etc.), right?
Yes, everything else happens. I think that the problem might be in the way I calculate currentHealth: currentHealth = startHealth - scoreScr.miss - scoreScr.crush; But, still, it shouldn't affect that I am changing it from another script.
Well to begin with i'd remove both GetComponent calls from the Update function, create 2 variables and cache them from the Start function. As it is you're getting the scoreSCR every frame which i'm sure you don't want, and the same goes for the player which will fire for every frame that health is below 1.
what exactly is miss and crush?
Answer by getyour411 · May 17, 2014 at 11:18 PM
Any change to currentHealth (as in +1) would be lost since PlayerHealth Update() recalcs it every frame. You need to redesign that bit
Your answer
Follow this Question
Related Questions
Change variable from another script - C# 1 Answer
accessing vars of other script doesn't work 1 Answer
Need to use 2 different language scripts. 1 Answer
Basic C# Public Variable Help 3 Answers
Static Variable Problem 1 Answer