i place a new question which should be more clear
How to pass a variabe to OnGUI() (could need Help)
Hi there, Im very new to Unity and C+ (2 week experion) but i dont get why my OnGUI gets a wrong value back
I have the public function, that i call for changing the "score" value when a GameObject is deatroyed.
ShowScore.Scoring ();
and the function
private int Score = 1;
public void Scoring(){
Score = 3;
Debug.Log (Score);
}
now when the function is called it shows in DebugLog 3. But my OnGUI which is in the same script gives me in the DebugLog 1 back.
void OnGUI()
{
GUI.Label(rect, "Score: "+Score, style);
Debug.Log (Score);
}
I changed the integer from private to public, but still the same Problem. What i am doing wrong?
I tried now for 3h i dont get it. So any help would be awesome
Why im doing this: I have a GameObject which reports to the ShowScore Script when it is destroyed and it should add 10 points to the Score variable which are shown in the OnGUI
Answer by karl_jones · Feb 09, 2016 at 12:05 AM
Do you have 2 scripts in the world? Each one is an instance, so if you have 2 scripts you have 2 different values for Score.
Post your full script.
Answer by seevenup · Feb 09, 2016 at 05:27 PM
so this is my code (i know its not pretty)
This is my HealhBarMonster script which shows the healthbar over the enemy. At the end i call the function to try add 10 points to the Score on the GUI using UnityEngine; using System.Collections;
public class HealthBarMonster : MonoBehaviour {
public float MaxHhealth = 100f;
public float CurrentHealth = 0f;
public float Score = 10;
public GameObject healthBar;
public Tower Tower;
public ShowScore ShowScore;
// Use this for initialization
void Start () {
CurrentHealth = MaxHhealth;
}
public void decreasehealth(float dmg){
CurrentHealth -= dmg;
float calc_health = CurrentHealth / MaxHhealth;
SetHealthBar (calc_health);
}
public void SetHealthBar(float health){
healthBar.transform.localScale = new Vector3 (Mathf.Clamp (health, 0f, 1f), healthBar.transform.localScale.y, healthBar.transform.localScale.z);
if (CurrentHealth <= 0) {
Tower.DeathReport ();
ShowScore.Scoring ();
Destroy (gameObject);
}
}
}
and here i try to set the Score to 3 (proof of concept)
public class ShowScore : MonoBehaviour {
//float ScoreCount;
private int Score = 1;
public void Scoring(){
Score = 3;
Debug.Log (Score);
}
void OnGUI()
{
int w = Screen.width, h = Screen.height;
GUIStyle style = new GUIStyle();
Rect rect = new Rect(0, 10, w, h * 2 / 100);
style.alignment = TextAnchor.UpperLeft;
style.fontSize = h * 2 / 100;
style.normal.textColor = new Color (0.0f, 0.0f, 0.5f, 1.0f);
GUI.Label(rect, "Score: "+Score, style);
Debug.Log (Score);
}
}
now i see in the DebugLog that when the function is called " ShowScore.Scoring ();" i get in the DebugLog 3 back. But on the GUI it shows me 1. I tried to change from private int to pulic int, but did not help. i think im missing something i dont see
EDIT: It looks that i really have 2 diffrent variable. When i call a function located in a other script, it does not have the same value, as when i call it in the same script.
how can i change this @karl.jones
Follow this Question
Related Questions
I need to activate a function in another script from a script on a different game object 0 Answers
How to give Vector3 position to object being instantiated 1 Answer
Changing a variable in another script with raycast 0 Answers
Only plus one to variable in other script 1 Answer
Problem with decrement 0 Answers