pickup object and add a score

hi, does anyone have any tips for me on how to have my FPS character pick up objects and add it to a score? I have tried to use the 3D platform tutorial scripts, but i really dont know enough about scripting to modify the tutorial scripts.

Can anyone help me? Would be highly aprechiated.

If you are adding just one to the var u can use

score++;

If you are adding a higher num like 3 or 3.2

score+=3;

(ie)

function OnMouseDown() {
  scriptName.score++;
}

thanks, and then i have to add a destroy(gameobject) if i want to remove the object right? As well as connecting the score to GUI?

However, i am getting an error on this script, saying:
“unknown identifier scriptName”
and
“expression cannot be assigned to”

Any clue??

Yes the scriptName is the name of the script you have your score code on. You will have to make the var static to be read/altered from the items script, thats one way a script talks to another. :smile:

As I read it, your code should look like:

function OnMouseDown() {
  scoreCount.score++;
}

score.Count is the name where you store the integers.

int score;

score = 0;

Adding a single increment to the value would be:

scoreCount++;

Let me know if i’m wrong :slight_smile:

thanks :slight_smile:

one more question by the way :slight_smile:
How to i connect this to GUI text?

And does anyone knoe how to connect the score too
time spent on the level?

Cause my character will get score for each object
pickup, at the same time there will be a timer (which i have).

Any help would be great :slight_smile:

If you’re using the GUI system (which you can read about here), you would just submit the score as content for whatever type of control you’re using (e.g. Label). If you have the score in numerical form, you can use ToString() (or the UnityScript equivalent - maybe the case is different?) to convert the score to string form.

Yes.

The details will depend on what behavior you want though. Do you want, say, 10 to be added to the score every 30 seconds, or something of that sort?

thank you for reply, about the having score and time, i guess i didnt think it through at first, but as this will be gathering objects which will give points, and do this in the least amount of time, i want the time to deduct from the score every 1 minute or something like that.

Can you help me??

It looks like you have a debug message in there that tells you when a ‘pick up’ item has been collided with. When the score goes up by two, does that message get output twice? Are you sure you don’t have two items right on top of each other?

I answered that question earlier in the thread, here:

You could probably also work it into your current code, e.g. (guessing at the UnityScript syntax here):

var startTime;
var oldMinutes = -1;


function Awake() {
   
   startTime = Time.time;
   
}


function Update () {

   var Time = Time.time - startTime;
   
   var minutes : int = Mathf.Floor(Time / 60);
   var seconds : int = Time % 60;
   var fraction : int = (Time * 100) % 100; 
   
   guiText.text = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
   
   if (minutes != oldMinutes) {
       ...decrease score here...
   }
   oldMinutes = minutes;
}

hmm i dont seem to quite get this, i searched around on how to connect my pickup script to a gui text, and heres the script so far:

function OnCollisionEnter(Collision : Collision){

** Debug.Log(Collision.gameObject.name+" collision entered");**
** Destroy(gameObject);**
** }**
** scoreScript.text = “score goes here”;**

this just removes game objects when the character collides with them. And the last code just strings “score goes here” into my gui text box. All works, but i want to have the gui text add points every time the character collides with a object.

Actually what i aim for as well, in addition to this, is to have my timer deduct points every minute or so.

Sorry im just not good at scripting at all. Can anyone help me with this? Would be very helpfull and appreciated.

Yes, I’m sure someone can; in fact, I’m trying to do just that :slight_smile:

So I take it you already have the value of ‘scoreScript.text’ successfully displaying in a GUI element of some sort?

If so, all you need to do is instead of assigning “score goes here” to this variable, assign the score itself (in text form).

In C#, it might look something like this:

scoreScript.text = score.ToString()

Or, if you wanted to include some text with the score, you could do something like this (untested):

scoreScript.text = string.Format("Score: {0}", score);

I imagine it will look similar in UnityScript (although I don’t know the details off the top of my head).

Works fine thanks. However, when i try to add score everytime my character collides with an object, with the code earlier in this thread:

var score:int;
function OnCollisionEnter(Collision : Collision){

** Debug.Log(Collision.gameObject.name+" collision entered");**
** Destroy(gameObject);**

** scoreScript.score++;**

** scoreScript.text = score.ToString();**

** }**

i get a reaction on the first object collided with only. When i try to collide with the next object, there is no add to the score.

Maybe it can help to show my gui text script as well, which is this:

static var text : String;
static var score:int;
function Update() {
guiText.text = text;

}

So the first script is in the pick up objects and the second is in the gui text script.

I really appreciate this, hope this makes sense to you :)[/b]

Could you post the complete script(s) from which that code is taken?

(Also, I think maybe you’re using the wrong tags when posting code; try ‘code’ tags instead.)

So this first script is added to the pickup objects in the level (script called “pickupScript”):

var score:int;

function OnCollisionEnter(Collision : Collision){
	 	 
    	Debug.Log(Collision.gameObject.name+" collision entered");
		Destroy(gameObject);
	   	    		
					scoreScript.score++;
		
		scoreScript.text = score.ToString();
}

and this one is added to my gui text (script called “scoreScript”):

static var text : String;
static var score:int;
function Update() {
   guiText.text = text;
      
}

I’m not sure what the ‘score’ variable in your ‘pickup’ script is for, but try changing this:

scoreScript.text = score.ToString();

To this:

scoreScript.text = scoreScript.score.ToString();

Thanks a lot!:slight_smile: Works good. Only thing i dont understand is that it jumps straight from 1 points to 3 points, and then continues normal after that. Any idea what causes this?

And im sorry to ask this, but do you know how to deduct points every minute, or something like that?
I already have the time working:

var startTime;


function Awake() {
   
   startTime = Time.time;
   
}


function Update () {

   var Time = Time.time - startTime;
   
   var minutes : int = Mathf.Floor(Time / 60);
   var seconds : int = Time % 60;
   var fraction : int = (Time * 100) % 100; 
   
   guiText.text = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction); 
   
    
}

It looks like you have a debug message in there that tells you when a ‘pick up’ item has been collided with. When the score goes up by two, does that message get output twice? Are you sure you don’t have two items right on top of each other?

I answered that question earlier in the thread, here:

You could probably also work it into your current code, e.g. (guessing at the UnityScript syntax here):

var startTime;
var oldMinutes = 0;


function Awake() {
   
   startTime = Time.time;
   
}


function Update () {

   var Time = Time.time - startTime;
   
   var minutes : int = Mathf.Floor(Time / 60);
   var seconds : int = Time % 60;
   var fraction : int = (Time * 100) % 100; 
   
   guiText.text = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
   
   if (minutes != oldMinutes) {
       ...decrease score here...
   }
   oldMinutes = minutes;
}

thanks a lot for the help, this was really helpfull:)

everyting works excelent, thanks again!