- Home /
My XP System Refuses To Work... Help Please
Hey Guys i need some help for some reason my experience system wont work, as far as i can remember this is the same way i had my code set out in a previous project(cant find this project, hence the need to recode) its not adding the xp and levels when i collide with the "XPOrbs" and its not destroying the xporb on contact when i just had the system set so that if i hit "Space" then i would gain XP and that worked perfectly but now nothing happens any help would be greatly appreciated
using UnityEngine; using System.Collections;
public class XPSystem : MonoBehaviour { public float minXp; public float xpNeeded; public float currentXp; public float currentLevel;
void Awake () {
}
void OnCollisionEnter(Collider collider)
{
switch (collider.gameObject.tag)
{
case "XPOrb":
Destroy (this.gameObject);
break;
}
}
void Update () {
if (collider.gameObject.tag == "XPOrb")
{
currentXp += 10f;
}
if (currentXp < minXp)
currentXp = minXp;
if (currentXp >= xpNeeded)
{
LevelUp();
}
}
void LevelUp() {
currentLevel ++;
currentXp = 0f;
xpNeeded += 100f;
}
}
Answer by HarshadK · Aug 21, 2014 at 08:42 AM
Everything inside your update loop should go into your switch case inside OnCollisionEnter. Since you are trying to check inside Update loop if the tag of the collided game object is XPOrb which will always be false since inside update there is no collision object to check against for your Update loop. Your that collision object from Update actually trying to reference the collision object passed as argument to your OnCollisionEnter, which you can not perform from Update.
void OnCollisionEnter(collision collision)
{
switch (collision.gameObject.tag)
{
case "XPOrb":
currentXp += 10f;
if (currentXp < minXp)
currentXp = minXp;
if (currentXp >= xpNeeded)
{
LevelUp();
}
Destroy (this.gameObject);
break;
}
}
Exactly, also, like you showed, OnCollisionEnter doesn't have a Collider reference as parameter but a Collision object and it shouldn't be named collider since that's the name of the shortcut property of the objects own collider.
As said by @Bunny83 updated the answer to use name of Collision object as collision.
Thanks for the info. :-)
Your answer
Follow this Question
Related Questions
mono develop dosen't open 0 Answers
can figure out why it starts at level 2 1 Answer
How to destroy obj B when pickup obj A? 2 Answers
My object won't destory 0 Answers
how to speed up terrain generation and destruction? 0 Answers