0

I have been making a game where you can die, I want an on screen death counter so I added this:

// Death Counter void OnGUI() { GUI.Label (Rect (0, 0, 100, 100), DeathCount); } 

But I keep getting an error message saying the name DeathCount does not exist in the current context, how to I get it to access this, in the script where DeathCount is I have this:

 public static float DeathCount = 0f; 

So why doesn't it pop up on screen?

(I also have the error "UnityEngine.Rect is a 'type' but used like 'variable'", I also need help on that but I thought that it might be affecting the main problem).

Sorry if this has already been answered I just couldn't find it after looking for over 1 hour.

4 Answers 4

1

This is C# and it has its rules on how you create new instances and how you make strings out of numeric values:

// Death Counter void OnGUI() { GUI.Label ( new Rect (0, 0, 100, 100), DeathCount.ToString() ); } 
Sign up to request clarification or add additional context in comments.

Comments

0
public GameObject ObjectHaveScript;//asign in the inspector void OnGUI() { String DeathCount ObjectHaveScript.GetComponent<NameOfScript>().DeathCount; GUI.Label ( new Rect (0, 0, 100, 100), DeathCount ); } 

this is the Oficial doc http://docs.unity3d.com/ScriptReference/Component.GetComponent.html,

regards;

3 Comments

It says I need a ; before the O in ObjectHaveScript? Am I doing something wrong?
String -With lowercase letter s , ps. you are using c# or unityscript ? it is for c#;
So far my script is: void OnGUI() { string DeathCount ObjectHavescript.GetComponent<GameManager>().DeathCount; GUI.Label ( new Rect (0, 0, 100, 100), DeathCount ); }
0

lets say DeathCount is in a class named counter like this

 public class counter : MonoBehaviour { public static float DeathCount = 0f; } 

so we can access it from another class like this and dont forget you need to put a string as second argument of GUI.Lablel

 public class otherClass : MonoBehaviour { void OnGUI() { GUI.Label (Rect (0, 0, 100, 100), counter.DeathCount.ToString()); } } 

Comments

0

Try this code, "myGameObject" is the name of the gameObject you want to access to the script, and "myScript" the name of the script

private GameObject test = GameObject.Find("myGameObject"); void OnGUI(){ private float dead = test.GetComponent<myScript>.DeathCount; GUI.Label ( new Rect (0, 0, 100, 100), DeathCount.ToString() ); } 

And that´s it, don´t hesitate to ask if you need more help :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.