- Home /
Make Score Go Up At a Collision
Okay I want it when a block collides with water, the player's score counter goes up by 1
I have a Gui Text object to start the Counter which just has
static var Counter = 0;
And on the water object I have a script to make the counter go up by 1 when it collides with a box that says
function OnCollisionEnter (other : Collision) {
if (other.gameObject.name == "smallcube")
Counter+=1;
}
But I'm still getting an error that says Unknown Identifier : 'Counter' Can anyone tell me what's going wrong here?
Answer by Joshua · Mar 07, 2011 at 12:33 AM
To access a variable that you have made static you first have to declare in the script the variable is from. So it would be NameOfTheScriptCounterIsIn.Counter+=1;
To make it more clear, if the Counter is in a script named ScoreCounter it would be:
ScoreCounter.Counter+=1;
By the way, it's good to get into the habit of not capitalising the first letter of a variable.
Could you please tell me why it's good to get into the habit of doing so? Personally I dislike the approach Unity has, although I can see some reasons to why (you'd not easily access static members). .NET coding guidelines suggest that all public members are pascal cased (starting with captial letter).
The reason I am following with unitys convention is that the entire framework seems to follow this, and I want to stay consistent with the rest of the community. If you look at any .net framework class you'll see they are all capitalized.
Funnily enough I was writing my own engine in Xna, and I wanted to try on some of the architecture found within Unity. Having a member with the same name as the type becomes a hassle, if you then want to access static members. For example, having a member called Physics that derive from Physics won't let you to access static members by Physics.SomeStatic$$anonymous$$ember.
Still isn't working for me. I got the error to go away but the zero never goes up when a box collides with the water
Statement, I said it would be a good thing because he seems like hes new to scripting, and it's good to force yourself to get into the habit of following the coding guidelines then. Once you're comfortable with it, like you obviously are, you can do whatever you want - but in the beginning it's practical if your code looks like the examples you see around you.
Answer by Tyler Starr · Mar 07, 2011 at 05:27 AM
I use this script:
var score = 0;
var scoreText = "Score: 0";
var mySkin : GUISkin;
function OnTriggerEnter( other : Collider ) {
Debug.Log("OnTriggerEnter() was called");
if (other.tag == "Asteroid") {
Debug.Log("Other object is a coin");
score += 25;
scoreText = "Damage%: " + score;
Debug.Log("Score is now " + score);
Destroy(other.gameObject);
GameObject.Find("Main Camera").animation.Play();
}
}
function OnGUI () {
GUI.skin = mySkin;
GUI.Label (Rect (100, 10, 500, 200), scoreText.ToString()); }
function Update(){
if( score == 100){
Application.LoadLevel(0);
}
}
it adds score to GUI skin when you collide with an object tagged Asteroid. Then when your score is 100 it returns to Level0.
-Hope this script will satisfy for your needs.
Your answer
Follow this Question
Related Questions
How to use a static var 2 Answers
Static variables explanation. 2 Answers
guitexture a static var? 2 Answers
set SmoothFollow target at runtime via static var 3 Answers
Increase Static Var gradually 1 Answer