This is not working for me. The score doesn’t update and the gameobject (coin) doesn’t destroy. The coin is tagged coin is marked a trigger. When I collide with it, it only states that the OnTriggerEnter has been called in the console, nothing else happens.
var score = 0;
var scoreText = "Score: 0";
function OnTriggerEnter( other : Collider ) {
Debug.Log("OnTriggerEnter() was called");
if (other.tag == "Coin") {
Debug.Log("Other object is a coin");
score += 5;
scoreText = "Score: " + score;
Debug.Log("Score is now " + score);
Destroy(other.gameObject);
}
}
function OnGUI () {
GUI.Label (Rect (10, 10, 100, 20), scoreText);
}
There has to be an issue within your scene setup Hollywoodjae026; I took the above script and dropped it in an empty project with a CharacterController and a Sphere with the above script attached, no issues on my end.
Did you create the tag for the coin and did you spell it “Coin” or “coin” ?
Coin, I am testing this script out using the unity car tutorial. I want to pick up coins while racing.
my point is that in the code you posted, it says “Coin”, but when you write it in your message, you spell it “coin”.
Tags are case sensitive. Are you sure your tag is spelled “Coin” as it is in the code?
If you spelled it “Coin”, then as omgitsalexl said, there must be something else wrong in your scene, but it is hard to think of what if the first Debug.Log message is printing but not the second.
I am not sure if there is an equivalent method in UnityScript from C#, but why don’t you try adding something similar to Debug.Log(“Collided object has tag of "” + other.gameObject.tag.ToString() +“"”); to see if you can find out what the tag of the collided object is.
Ok, I just created an empty scene and followed these steps:
- Created an Plane
- Added a FP Controller
- Added a point light
- added a sphere
- checked the Is Trigger box in the properties of the sphere
- chose the tag “Coin” (with a capitol C)
- Dragged the script onto the sphere (coin) the name of the script is “pickup2.js”
- Hit the play button
- walked into the sphere and the console printed
- Nothing else is printed and the sphere remains in place and does not destroy.
That is my current setup
Could it be the other scripts from the Car Tutorial causing problems? Although they are not being used currently they are still apart of the project since all I did was create a new scene to simplify my setup for testing.
I think I may have just realized where we were all going wrong, why don’t you change the line if (other.tag == “Coin”) { to the following if (other.tag == “Player”) { and give the script another run through. What I just noticed is that if you’re attaching this script to your coin, it is never going to collide with another coin, it is going to collide with the player. Let me know if this works, it should.
It destroys the Player lol
You were right I was attaching it to the coin insteqad of the player. When I attach it to the player it works fine. I just had a Homer Simpson Moment…
It’s alright, we all have those moments. Some times it is the most simple of errors that trip us up for the longest of times. Glad you were able to get it sorted out though!
Exactly, thanks!! Now all I have to do is figure out how to change the fonts to custom fonts in the score counter…Any ideas
You need to bring the *.ttf that you want to use into your assets folder, let Unity do its importing and processing, and from there you are able to change the font material within the inspector.
Using my current script there is now way for me to change the fonts to my own
They basically told you how to do it… Make a “coin” Add this script:
var adds = 5;
function OnTriggerEnter( other : Collider ) {
if (other.tag == "Player") {
other.gameObject.SendMessage("AddScore", adds, SendMessageOptions.DontRequireReceiver);
Destroy(gameObject);
}
}
On your car… (more specifically the object that has the collider on it) Add this script:
var score = 0;
var scoreText = "Score: 0";
function AddScore( amount ) {
score += amount;
scoreText = "Score: " + score;
}
function OnGUI () {
GUI.Label (Rect (10, 10, 100, 20), scoreText);
}
Debug the whole thing and run it.
Every time you run over a “coin” trigger it will add score to your car and display it. It will then destroy the coin.
I have that portion working now I need to figure out changing the Fonts to a custom one, I imported new fonts but I am not using a Gui Text or 3d Text so I have no idea how to change the fonts
var myFont : Font;
OnGUI() {
GUI.skin.label.font = myFont;
/*
the same works for all GUISkin options:
label
button
box
window
toggle
etc. on the script reference.
*/
}