calling for counter script on collision

I am trying to create a counter on GUItext that counts +1 on collision.

Counter script:

static var score : int = 0;
 
function Update () {
 score++;
 guiText.text = "Counter is: "+score;
}

the counter script is attatched to the GUItext.

how can i call this script in my collision??

function OnCollisionEnter(col : Collision){
 
  if(col.gameObject.name == "Target"){
}

i would do something more like this

var score : int = 0;

function OnCollisionEnter(col : Collision){
	if(col.gameObject.name == "Target"){
		score += 1;
	}
}

function OnGUI () {
	GUI.Label( Rect(Screen.width - 20, Screen.height - 20, 100, 20), "Counter is: " + (score));
}

(attach this to your collider)

NameOfScript.score++;

Because score is static, you only need to reference the name of the script in order to use it. I believe that GUItext is created every frame or so, (Correct me if I’m wrong) and it will update accordingly.

EDIT: JM Studios, wouldn’t that be illogical? Why would the collider want to know the overall score?

ps, dont use the GUIText with this script. this script does that automatically

thankyou so much…

@Dman: I also thought ‘NameOfScript.score++;’ would work fine but apparantly not…

@JMStudios… legend I have been working on this for ages and you cracked it in less than 10mins. Thanks.

your very welcome. for help in future GUI related things, go here http://unity3d.com/support/documentation/Components/GUI%20Scripting%20Guide.html
thats how I learned that stuff (PS, if you want to put a number (int, float) into a GUI element, put the name of the number in ())

I don’t see any reason it wouldn’t work. I strongly suggest not to put the score in with a collider, as that is, in my opinion, very illogical and will only lead to hard to manage code.

Here’s my attempt at writing the script:

static var score : int = 0;

function OnGUI () {
	GUI.Label( Rect(Screen.width - 20, Screen.height - 20, 100, 20), "Counter is: " + (score));
}

And when you have a collision, NameOfScript.score++;

The script is not attached to a GUI element, as all of Unity GUI is created each frame, through script. Again, I strongly suggest you look back into this, but if you still cannot get it working, JM Studio’s method is “acceptable” in my opinion.

what do you mean [quote=“DanielQuick, post:7, topic: 432391, username:DanielQuick”]
JM Studio’s method is “acceptable”.
[/quote]
it looks like you just copied my script and deleted the

function OnCollisionEnter(col : Collision){
	if(col.gameObject.name == "Target"){
		score += 1;
	}
}

part of if?!?

Yes, I did pull most of the code out of yours, but I changed an important piece. The fact that it is not attached to a collider. I could have simply explained my point by referencing your code, but I decided to separate it in order to separate our ideas. I’m sure your way would work in many situations, but, in my opinion, it is very bad form to have the score attached to a collider.

To summarize: Your code was correct, however, I believe that there is a better way of implementing it.

ok, but you could’ve said, JM Studio’s method is “acceptable” in my opinion. rather than

Edited. Sorry if I offended you in any way. It was not what I intended to do.

ok, thank you. you are probably right on that, but i was just trying to do it on the fly, and i didnt think very much about it.