Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Brennan · Aug 20, 2010 at 05:54 AM · levelguitextexperiencexp

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

basically i have this script

`var guiTextEXP : GUIText; var accumulatedExperiencePoints : int = 0; var levelExpRequirements : int = 1000; var currentLevel : int = 1;

function Update () {
if(accumulatedExperiencePoints >= levelExpRequirements) { levelExpRequirements += 1000; currentLevel += 1; }
guiTextEXP.text = accumulatedExperiencePoints+"/" + levelExpRequirements + "|" + currentLevel; }`

and it works fine but now i need something to make it give me a certain amout of xp per kill...

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
2
Best Answer

Answer by · Aug 20, 2010 at 06:43 AM

Updated

Broadly speaking, you'd probably want each enemy to have a script containing variables dictating their health/xp. Then every time you applied damage to them, you'd check if their health was <= 0, and if so, kill them (death sound, etc) and increment the player xp by the specified amount.

Looking at your code (link in comments), you could add a variable to the EnemyDamageReceiver (assuming it's attached to every enemy).

var expVal : int = 20;

Then in ApplyDamage(), you'd add a line to find the Player, get the ExperienceSystem component (assuming it's attached to the Player) and then GiveXP to a certain value.

if (hitPoints <= 0.0) {
    GameObject.Find("Player").GetComponent(ExperienceSystem).GiveXP(expVal);
    // Start emitting particles
    (everything else here...)
    }

In the ExperienceSystem, you'd need to add a GiveXP function. Mike provided a decent example in the link I've commented, so I'll reuse it here.

function GiveXP(addedXP : int) { if (addedXP < 0) { Debug.Log("Can't remove exp!"); return; }

 xp += addedXP;

 if (accumulatedExperiencePoints &gt;= levelExpRequirements)
 {
     levelExpRequirements += 1000;
     currentLevel += 1;
     guiTextEXP.text = accumulatedExperiencePoints+"/" + levelExpRequirements + "|" + currentLevel;
 }

}

Note that I also extracted the level-up check from your Update() loop, because it's not necessary to check that every frame - only when the player actually gains experience.

Hope this sets you on the right track.


You seem to keep asking the same question, adding slightly more detail each time (Q18343: Experience System & Q18579: Experience System). Your script here is just a copy/paste of xToxicInferno's answer from the second link. Do you have any other code of your own?

Happy to take a look at bugs in any code you might have, but take a crack at it first. Or, if you're inexperienced with javascript/games-programming, you might want to try one of the tutorials to get a hang of Unity.

Comment
Add comment · Show 6 · 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 · Aug 20, 2010 at 07:19 AM 1
Share

You've left out a lot of detail that would actually make it possible to write code that directly helps you. Do you have Enemy GameObjects? Do they have scripts on them? Requesting your code is so we can help, not s$$anonymous$$l it!

$$anonymous$$ike has written a good answer here: http://answers.unity3d.com/questions/10505/variables-acesssed-by-other-scripts-and-change-int-values that gives an example of how to do what I described. You should definitely also read through the Reference Documentation (http://unity3d.com/support/documentation/ScriptReference/index.html) and work through a tutorial or two.

avatar image · Aug 20, 2010 at 07:46 AM 0
Share

Okay, so in the EnemyDamageReceiver.js you could add a variable that stores the xp. Then in ApplyDamage() where you 'Start emitting particles' (enemy is dead), you increment the Player's experience by the amount that the enemy gives. I would also change the level-up system so it only checked when you actually received XP. I'll update my answer accordingly.

avatar image · Aug 20, 2010 at 09:43 AM 0
Share

Doh, I didn't copy/paste correctly from your script! Updated my answer - missed a line from GiveXP.

avatar image · Aug 20, 2010 at 10:23 AM 0
Share

In my experience, those errors are usually indicative of an error further up the page. $$anonymous$$ake sure you've got equal numbers of { and } ? The script looks fine to me...

avatar image Brennan · Aug 20, 2010 at 02:13 PM 0
Share

ok i checked it out and was able to get rid of the error but there 2 more problems... gui text still dosnt change (

Show more comments

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Xp Calculation Bar 1 Answer

Dynamic Level Progress Bar 1 Answer

Experience system for multiple skills 1 Answer

Experience System 1 Answer

xp math equation help 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