x


Increasing a score value?

Hello, this is the script for a projectile in my game. I want to make it so that when Object A collides with an object with tag B the score is increased, to put it simply, why doesn't this work?

> function OnTriggerEnter (other :
> Collider) {
>     if (other.gameObject.CompareTag ("Wall"))

{

    Destroy (gameObject);
};
if (other.gameObject.CompareTag

("Enemy")) { Destroy (gameObject); Destroy (other.gameObject); { score += 1;
if (score == 5) { displayScore(score); } };} function displayScore(int score)

{ GUI.Label(Rect(0, 0, 50, 50), score); }

more ▼

asked Nov 18 '10 at 06:04 AM

Tyler 2 gravatar image

Tyler 2
1.1k 211 246 264

The indentation and formatting of your post is all messed up, I unfortunatly don't have the permission to edit it for you so I would kindly ask you , or another with the permission to do so. And no I sadly don't have the answer for you

Nov 18 '10 at 07:59 AM Proclyon
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

static var Score : int;

now you can access this variable from any script by using scriptname.Score, being scriptname the name of the script you declared the variable into.

more ▼

answered Nov 18 '10 at 11:25 AM

Bravini gravatar image

Bravini
1.1k 6 11 29

also, if your score var is in a script attached to the projectile, you gonna have multiple scripts in the scene, which will be destroyed along with the projectile when they collide. Put the score in the player or any other object you know that wont be destroyed.

other solution would be to use http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html

Nov 18 '10 at 11:28 AM Bravini
(comments are locked)
10|3000 characters needed characters left

do you want show score?

recommand below

{

int score = 0;

function OnTriggerEnter ( )

...

score++;

...

}

void OnGUI() { if (score == 5)

{

     GUI.Label(Rect(0, 0, 50, 50), score); 

}

}

}

more ▼

answered Nov 18 '10 at 08:08 AM

jameslee0227 gravatar image

jameslee0227
79 2 2 9

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x822
x277
x273

asked: Nov 18 '10 at 06:04 AM

Seen: 1147 times

Last Updated: Nov 18 '10 at 06:04 AM