x


How to update scoreline on a HIT?

Hello,

I am working on a Projectile Motion and when the ball hits the enemy I wish to increase score whenever the enemy is killed. Hence I have an Enemy prefab, which is instantiated through script (on FIRING).

Code:

Collision checking script (cannontrans.js)

var ene_hit : la_point;
function OnCollisionEnter(hit : Collision)
{
   randomrange();
   if(hit.gameObject.tag == "cannonball")
   {
        transform.position = Vector3(r_enepos,2,0);

   }
}

Code Explanation:

Whenever it gets a hit I wish to move position of the object. Hence here I would also like to increase the score to display on the UI using OnGUI function. The above code it attached onto the enemy Prefab I was talking earlier about.

Perception:

I wish to increase the score on a different script. The reason is - I have lots of scenes and every scene requires it own scoreline.

Request:

I humbly request someone to help me on this issue. I am not sure how to proceed. My main aim at the end of the day is to have the prefab initiated through script and update the score line whenever there is a hit. Kindly let me know if I have to detailed certain aspects out too.

cannontrans.js -> http://codepad.org/B4Djc6ve

THANK YOU VERY MUCH

more ▼

asked Dec 08 '11 at 11:19 PM

Karsnen gravatar image

Karsnen
753 28 47 56

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first
var obj : GameObject;

//In collision enter:

var scoreScript = obj.GetComponent(SCRIPT_NAME);

scoreScript.score = scoreScript + 1;
// End collision enter;

//In SCRIPT_NAME on the obj that is where the score is or w/e.
var score = 0;

//OnGUI;
{
//Draw w/e with the score var.
}

This should work... Mostly psuedocode, but basically you pass in the object that has the GUI on it, store it as obj. Then you access the component (The script) of the object that contains the GUI and in result, the score variable, then just ++ it.

I think this is what you want, no?

Seems the code you have already handles the random movement.

If not, reply, I'll try to help further.

more ▼

answered Dec 09 '11 at 01:07 AM

Justin Warner gravatar image

Justin Warner
6.3k 19 27 65

(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:

x5270
x3568
x670
x280
x231

asked: Dec 08 '11 at 11:19 PM

Seen: 497 times

Last Updated: Dec 09 '11 at 01:07 AM