both are different scripts, by my understanding the first script is placed on the object you want to be the trigger the “enemy” and the second is what displays the score, iv placed this on the camera
It looks to me like you have the score associated with each individual bullet. When a bullet collides, it increases the score (only saved to that one bullet) and then destroys itself. Your GUI call then never references the object that MyScoreCounter is attached to.
What you probably want to do is have another script on the player called PlayerScore with a static MyScoreCounter variable. that way, each time a bullet collides, it says
PlayerScore.MyScoreCounter += 10;
Do the same for the GUI call (PlayerScore.MyScoreCounter) and it should work better. Assuming that was your problem to begin with.
The score variable is already static; as such, it is not associated with any particular instance of the script.
@The OP: You said the first script is attached to the ‘enemy’ objects, but the tag you’re checking is the tag for the object to which the script is attached. What you probably want to do is check the tag for the object that’s just been collided with, which you can access (indirectly) via the (optional) argument to OnCollisionEnter() (check the docs for details).
avoid this: static var MyScoreCounter : int = 0; it´s not a clean solution…
attach something like a levelcontroller which manages this part of your game and use sendMessage to send the value … this is always a cleaner solution and you won´t loose score points!
in this method it´s could happen that your score value isn´t send to your scorecounter before your object gets destroyed. it´s possible that your object hits your enemy but counts up no score points. believe me, i tryed this way and it´s not a clean solution:) first make sure that your points were “delievered”…after that: destroy your object.
I’d recommend getting your current version working before you try to change the whole architecture. (As I said in my earlier post, I suspect the problem is that in your first script, you’re checking the wrong tag.)
That’s not what I mean. I mean that you’re checking the tag for the game object that the script is attached to, not the game object that that object has collided with. In other words, you’re checking the tag for the wrong object.
These problems are generally easier to solve if you use the debugger (in 3.x) or add debug output using (e.g.) Debug.Log() to determine what’s going wrong. For example, in this case you might add the following output (untested):
function OnCollisionEnter(Bullet : Collision)
{
Debug.Log("OnCollisionEnter() called...");
Debug.Log("Tag we're checking is " + gameObject.tag);
if (gameObject.tag == "Bullet"){
Debug.Log("Increasing score");
MyScoreCounter += 10;
Destroy(gameObject);
}
}
The output should then give you a hint as to what’s wrong.
At least one of your objects needs a rigidbody attached to it for OnCollisionEnter to work properly.
Also, your GUI script isn’t going to know where to grab the variable ‘MyScoreCounter’.
You should declare it in that script as static, and then call it from other scripts as scriptName.MyScoreCounter, replacing ‘scriptName’ with whatever you named your GUI script.
Also, rather then apologizing for not having a clue, check the many tutorials and reference manuals here: