• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by wheezy602 · Aug 21, 2014 at 08:38 AM · destroy objectxpleveling

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;
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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;
  
     }
  
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Aug 21, 2014 at 09:37 AM 0
Share

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.

avatar image HarshadK · Aug 21, 2014 at 11:37 AM 0
Share

As said by @Bunny83 updated the answer to use name of Collision object as collision.

Thanks for the info. :-)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How can i make it give u a certain xp everytime u kill someone? 1 Answer

Health Meter (image array) with Level Up feature. 1 Answer

Destroy Projectile on Impact? 3 Answers

How to destroy two gameobjects when they collide? 3 Answers

Checking if first item in array has been destroyed 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges