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 OkieDokie · Feb 05, 2014 at 11:44 AM · javascript

Why is my Experience Script not working?

So i have been working on improving my scripting for a while and i am trying to implement an experience system. Just a basic, kill something and it gives experience. Below i have put my ExperienceScript and EnemyHealth script.

ExperienceScript

     var currentExperience : int;
     var maxExperience : int = 100;
     
     function GiveExperience ()
     {
         var experienceGain = EnemyHealth.experienceGain;
         currentExperience += experienceGain;
         Debug.Log("You Gained Experience");
         if(currentExperience >= maxExperience)
         {
             Debug.Log("LEVEL UP!");
         }
     }

EnemyHealth

 var Health = 100;
 static var experienceGain : float = 50;
 
 function Update ()
 {
     if(Health <= 0)
     {
         Dead();
     }
 }
 
 function ApplyDamage (Damage : int)        
 {
     Health -= Damage;
 }
 
 function Dead ()
 {
     Destroy(gameObject);
     Experience();
 }
 
 function Experience ()
 {
     var experienceScript : ExperienceScript;
     experienceScript.GiveExperience();
 }


EDIT: Sorry about that, i am getting this error when the enemy gets to 0 health, instead of dieing i get this

 NullReferenceException: Object reference not set to an instance of an object
 EnemyHealth.Experience () (at Assets/Scripts/EnemyHealth.js:28)
 EnemyHealth.Dead () (at Assets/Scripts/EnemyHealth.js:21)
 EnemyHealth.Update () (at Assets/Scripts/EnemyHealth.js:9)
 


Comment
Add comment · Show 1
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 GameVortex · Feb 05, 2014 at 11:56 AM 0
Share

You should add some more information. What is not working? How do you expect it to work? Is it not logging? Does it output errors? What have you tried to get it to work?

Only saying that things do not work and giving us some code is generally a bad practice, the more information we have the easier and more likely it is that anyone can and will give you a proper answer. =)

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by poncho · Feb 05, 2014 at 09:22 PM

you are trying to execute a method of a class that cease to exist before executing

 Destroy(gameObject); << Destroys to the class and anything next this point would not be executed
 Experience(); << Method is unreached code, since gameobject with this script is destroyed

also as NickP_2 said, you are instantiating a new experienceScript Object, but you could make it static OR calling the script already in the scene

  1. Find the gameobject who has the experienceScript ie GameObject.Find("WhoeverHasExpScript")

  2. Get the script component from it and use it GameObject.Find("WhoeverHasExpScript").GetComponent().GiveExperience();

this code is in C#, but JS has something similar

Comment
Add comment · Show 1 · 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 OkieDokie · Feb 06, 2014 at 01:14 AM 0
Share

Thank you so much, i added in the piece of code you said into the dead function and it works now. Again thank you so much

avatar image
0

Answer by NickP_2 · Feb 05, 2014 at 03:14 PM

You're creating a new experienceScript each time you kill a monster. I suggest you making the experienceScript static :)

Comment
Add comment · 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

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

What is wrong with this script? 2 Answers

BCE0044 expecting ), found 'script' and BCE0043 unexpected token ) 2 Answers

Amnesia style 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