GUI script

I cant figure out how to write a script that counts down the number of enemies left in a game. it needs to manipulate some GUI text, and it needs to link to level1status script to find the # of enemies left.

Make an empty game object. Give it a special script, EnemiesCounter. In the Start function, count every enemy in the scene. If they all have a script called Enemy, use FindObjectsOfType.

Enemy script has a reference to EnemiesCounter. Whenever an enemy is removed, call a function that decrease the counter, and make a test to see how many enemies are left.

what i have is a turretcount script, and a level1status script, and a turretcontrol script (the enemies are the turrets).

level1status:

static var turretsRemaining : int = 11;

function Update () {
if(turretsRemaining == 0)
{
Application.LoadLevel(0);
}
}

turret control:

var target : Transform;
var damp = 1.0;
var fireballPrefab : Transform;
var savedTime = 0;
var explosion : Transform;

function OnTriggerEnter( hit : Collider ) {
if(hit.gameObject){
Destroy(hit.gameObject);
var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
level1status.turretsRemaining -= 1;
Destroy(gameObject);
}
}

function Update () {
if(target)
{
var rotate = Quaternion.LookRotation(target.position - transform.position);

transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);

var seconds : int = Time.time;
var oddeven = (seconds % 2);

if(oddeven)
{
fire(seconds);
}
}
}

function fire(seconds)
{
if(seconds!=savedTime)
{
var fireball = Instantiate(fireballPrefab, transform.Find(“fireball fire”).transform.position, Quaternion.identity);

fireball.gameObject.tag = “enemyProjectile”;
fireball.rigidbody.AddForce(transform.forward * 4000);

savedTime=seconds;
}
}

i want to know what to type for the turret count script, this is what i have:

function OnGUI () {
if (level1status.turretsRemaining) {
TurretGUI.Text = level1status.turretsRemaining();
}
}

it counts the turrets correctly, i just need it to display remaining turrets on a GUI

You can use OnGui() and a GUI control such as Label() for this.

if(hit.gameObject){

This test is useless. Of course you are going to have a game object, since a game object triggered the OnTriggerEnter.

http://unity3d.com/support/documentation/Manual/Game%20Interface%20Elements.html

Read all chapters (except Extending Unity Editor), and use what suit your needs.

what?

i did what i thought you told me to do, it didnt work, can you try to give me an example that will work with what i already have down?

Did you read at least the content of the link ? -_o

yes, i read it, but it didnt help very much, it was talking more about menu stuff, not game GUI’s

You can make box, fill them with a Texture, and a String…

Menu stuff? I though you wanted to display how many turrets are remaining? If that is in fact the case, then GUI or GUILayout is exactly what you’re looking for.

(If I’m misunderstanding the problem, perhaps you could clarify.)

this hasnt really been helpful very much

no, i was talking to the other person, you are right on what i want, i dont want stuff on menus, i need a way on how to count the remaining turrets

Am I becoming a big fat dumb chicken, like my avatar, and misunderstanding things, or…?

your misunderstanding me, i think

Ok, let’s start over at the beginning :slight_smile: I was under the impression that you’d already figured out how to keep track of the number of remaining turrets, and are now just trying to figure out how to display that number on screen.

Can you describe exactly and in detail what, specifically, you need help with at this point?

what i have is a turretcount script, and a level1status script, and a turretcontrol script (the enemies are the turrets).

level1status:

static var turretsRemaining : int = 11;

function Update () {
if(turretsRemaining == 0)
{
Application.LoadLevel(0);
}
}

turret control:

var target : Transform;
var damp = 1.0;
var fireballPrefab : Transform;
var savedTime = 0;
var explosion : Transform;

function OnTriggerEnter( hit : Collider ) {
if(hit.gameObject){
Destroy(hit.gameObject);
var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
level1status.turretsRemaining -= 1;
Destroy(gameObject);
}
}

function Update () {
if(target)
{
var rotate = Quaternion.LookRotation(target.position - transform.position);

transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);

var seconds : int = Time.time;
var oddeven = (seconds % 2);

if(oddeven)
{
fire(seconds);
}
}
}

function fire(seconds)
{
if(seconds!=savedTime)
{
var fireball = Instantiate(fireballPrefab, transform.Find(“fireball fire”).transform.position, Quaternion.identity);

fireball.gameObject.tag = “enemyProjectile”;
fireball.rigidbody.AddForce(transform.forward * 4000);

savedTime=seconds;
}
}

i want to know what to type for the turret countscript, this is what i have:

function OnGUI () {
if (level1status.turretsRemaining) {
TurretGUI.Text = level1status.turretsRemaining();
}
}

thats what i have, i need to create an effective turretcount script the one i have doesnt work

Right, but you need to describe for us what this script is supposed to do. Earlier you said:

Does that mean that the part you need help with is displaying the number on screen? Also, what’s ‘TurretGUI.Text’? I don’t see that declared in your code anywhere.

no, i need a script to make a GUI text element display the remaining turrets the player needs to destroy

i fixed that declaring issue, but now it is giving me and error message “Assets/scripts/trurretcount.js(5,42): BCE0077: It is not possible to invoke an expression of type ‘int’.”