• 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 objectlevelingxp

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Gui Text not showing up when I click play... 1 Answer

Need help with a proficiency system(experience system) 1 Answer

I want this script to destroy the enemy 1 Answer

dropobject script not destroying parentgame game object 2 Answers

Remnant Empty Game Objects for Static Scripts 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges