Hi so I would like to set up a score system for when my object collides with a “Enemy” it makes a score counter on screen go up anyone have any Idea of how to go about this. All Help is appreciated. Thanx And if you could explain in C# it would be very helpful.
Here is small snippets of code in javascript u can covert in csharp:
function OnCollisionEnter(col:Collision){
if (col.transform.tag==“enemy”){
//this line is for explosion
Instantiate(explosion,col.transform.position, Quaternion.identity);
Destroy(col.gameObject);
//code for increase ur score, take a static variable and increase ur scores accordingly
}
}
Ok so I made some new code but i get:
Assets/Score/ScoreCollisioncode.js(11,10): BCE0044: expecting (, found ‘OnCollisionEnter’.
as an error message
here is my new code:
#pragma strict
var Score : int = 0;
function Start () {
}
function Update () {
function OnCollisionEnter(col:Collision){
if (col.transform.tag=="enemy"){
Score++;
function OnGUI() {
GUI.Label(new Rect(20, 20, 150, 20), System.String.Format("SCORE: {0}", Lives));
}
}
}
You have no close braket on the Update Function.
You can’t have a function inside another function.
Sure it’s a simple mistake lol.
#pragma strict
var Score : int = 0;
function Start () {
}
function Update () {
}
function OnCollisiosnEnter(col:Collision){
if (col.transform.tag=="enemy"){
Score++;
function OnGUI() {
GUI.Label(new Rect(20, 20, 150, 20), System.String.Format("SCORE: {0}", Lives));
}
}
}
ok i did that but now i have three other errors:
(17,18): BCE0044: expecting (, found ‘OnGUI’.
(17,25): UCE0001: ‘;’ expected. Insert a semicolon at the end.
(18,96): BCE0044: expecting :, found ‘;’.
the OnGUI function should be outside the OnCollisionEnter function, and also you spelled OnCollisionEnter wrong lol
ok i fixed that but I’m still getting the same errors just a row down
lol
@ OP , Don’t worry, we will get ya fixed.![]()
Sorry for all the trouble guys I’ve been scrambling to finish this for my 8th grade Final ( If I don’t pass no high school) so i really am trying to get stuff done
And thank you so much
This is my current code
#pragma strict
var Score : int = 0;
function Start () {
}
function Update () {
}
function OnCollisionEnter(col:Collision){
if (col.transform.tag=="enemy"){
Score++;
}
function OnGUI() {
GUI.Label(new Rect(20, 20, 150, 20), System.String.Format("SCORE: {0}", Score));
}
}
And I’m still getting the same error messages from above but one line down (e.g. (19,18) ----> (20,18) )
You need a close braket for OnCollisionEnter.
Every function needs open and close brakets, and every condition need then if they hold more than one line of statments (ie)
if(myCarNeedsCleaning)
print(“handMeTheSoap”);
if(myCarNeedsCleaning) {//This is sometimes called a block of code
print(“handMeTheSoap”);
print(“handMeTheAndTheBucket”);
}
Don’t take this the wrong way, but I think you picked too difficult of a project for your skill level. The errors you have are programming syntax errors that are not related to unity. You should step back and pick something easier. Plus you keep asking for c sharp, then you post unity script. You should focus on learning one language, it will be easier for you.
ok that works now and it is showing up but now when i collide with the enemy it is not changing the score from 0 to 1 (Not increasing)
@ Hoff
Ya IK it was a hard project and all that but i really wanted to do good on this (Again because of the no high school thing) and i only have 2 weeks to finish this project so sorry if I am making anyone mad because of my shit programming skills I just wanted to impress my teachers.
But after all this I’m going to go back to the beginning and learn java so…
Well I am not familiar with the way that you are trying to change your Score int to a string. But what you could do is make a ScoreString variable and in the update function write.
ScoreString = Score.ToString();
then your GUI.Label would be GUI.Label(new Rect(20, 20, 150, 20), "Score is " + ScoreString);
and i think that should work.
He needs to set the tag is probably all
@Hoff … Im not retarded the tag is set
There is no way to know what is wrong, put some debug text in the code and figure out where the problem is
I did what you said Monster but it says that Score String is unidentified even though its in the update function…ugh
Hm… well you have col.transform.tag, but i usually use col.gameObject.tag and that always works for me. It could be that… idk