- Home /
My orb and gem counter are counting simultaneously?
I've been working on a platformer where the player collects orbs and gems. They are to be collected as separate objects in the game. However, when the player collects one, whether it be a gem or an orb, they count simultaneously, at least on my GUI. Here are the scripts I'm using (Javascript):
GUI
#pragma strict
var gemIcon : Texture2D;
var orbIcon : Texture2D;
var customSkin : GUISkin;
function Start () {
}
function Update () {
}
function OnGUI()
{
GUI.skin = customSkin;
var gemNumberName = GemCounter.gemCounter;
var gemName = gemNumberName.ToString();
var orbNumberName = OrbCounter.orbCounter;
var orbName = orbNumberName.ToString();
GUI.Box(Rect(10, 10, 90, 50), gemIcon);
GUI.Box(Rect(10, 70, 90, 50), orbIcon);
GUI.Label(Rect(10, 10, 100, 50), gemName);
GUI.Label(Rect(10, 70, 90, 50), orbName);
}
Orb trigger
#pragma strict
var Player : Transform;
function Start () {
}
function Update () {
}
function OnTriggerEnter()
{
GameObject.FindWithTag("Orb");
Destroy(gameObject);
}
Orb Counter
#pragma strict
static var orbCounter = 0;
function Start () {
}
function Update () {
}
function OnTriggerEnter()
{
orbCounter += 1;
}
Gem Trigger
#pragma strict
var Player : Transform;
function Start () {
}
function Update () {
}
function OnTriggerEnter()
{
GameObject.FindWithTag("Gem");
Destroy(gameObject);
}
Gem Counter
#pragma strict
static var gemCounter = 0;
function Start () {
}
function Update () {
}
function OnTriggerEnter()
{
gemCounter += 1;
}
If you can help me figure this out it would be highly appreciated.
Answer by getyour411 · Mar 12, 2014 at 01:27 AM
The empty methods in your code should be removed - since you aren't doing anything in Start or Update in a few of those, get rid of them.
Your issue here is you what these scripts are attached to. You didn't say, but it's likely its the player so your OnTriggerEnter events are firing for anything (since the GameObject.Find code is pointless/does nothing).
The GUI is attached to an empty game object, the two counters are attached to the player. The triggers are attached to the gem/orb. Also, what do you mean by thingie.tag? lol
TBH: If that second link doesn't help you, I'm not sure what will.
OrbCounter
function OnTriggerEnter(other :Collider)
{
if(other.name == "Orb")
orbCounter ++;
}
I work in C# so you might need a JS fix on syntax.
The second link actually did help. I was confused cus you said thingie lol. I figured out what you meant, that it was the collider thing lol. Anyway I prefer C# but I followed a tutorial on coin collecting on youtube and it happened to be js. And since it didn't include EVERYTHING that I'm trying to do, I had to kind of wing it. But thanks, saved me a lot of frustration!
Your answer
Follow this Question
Related Questions
pickup counter and timer help 0 Answers
ammo counter, counts wrong 1 Answer
Setting Scroll View Width GUILayout 1 Answer
GUI Counter Based on Animation or RaycastHit 0 Answers
How to display counter 1 Answer