• 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
Question by cwaite84 · Nov 11, 2013 at 02:02 AM · javascriptplayerexperience

Simple EXP Script In Javascript

Okay so I have taken previous examples and modified them briefly to try and get this to work. I have two scripts one for the enemy and one for the Player. Here is my enemy script.

 #pragma strict
 #pragma downcast
 
 public var hitPoints = 100.0;
 var deathEffect : Transform;
 var effectDelay = 0.0;
 private var gos : GameObject[];
 var multiplier : float = 1;
 var deadReplacement : Rigidbody;
 @HideInInspector
 var playerObject : GameObject;
 var useHitEffect : boolean = true;
 var expVal : int = 20;
 @HideInInspector
 var isEnemy : boolean = false;
 var expScript : ExperienceSystem;
 
 function ApplyDamage(Arr : Object[]){
     //Info array contains damage and value of fromPlayer boolean (true if the player caused the damage)
     //Find the player if we haven't
     if(Arr[1] == true){
         if(!playerObject){
             playerObject = GameObject.FindWithTag("Player");
         }
         if(useHitEffect){
             playerObject.BroadcastMessage("HitEffect");
         }
         
     }
     
     // We already have less than 0 hitpoints, maybe we got killed already?
     if (hitPoints <= 0.0)
         return;
     var tempFloat : float;
     tempFloat = Arr[0];
     //float.TryParse(Arr[0], tempFloat);
     hitPoints -= tempFloat*multiplier;
     if (hitPoints <= 0.0) {
          
         var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
         if (emitter)
             emitter.emit = true;
 
         Invoke("DelayedDetonate", effectDelay);
         
     }
 
     
 }
 
 function ApplyDamagePlayer (damage : float){
     //Info array contains damage and value of fromPlayer boolean (true if the player caused the damage)
     //Find the player if we haven't
     if(!playerObject){
         playerObject = GameObject.FindWithTag("Player");
     }
     if(useHitEffect){
         playerObject.BroadcastMessage("HitEffect");
     }
     
     // We already have less than 0 hitpoints, maybe we got killed already?
     if (hitPoints <= 0.0)
         return;
     //float.TryParse(Arr[0], tempFloat);
     hitPoints -= damage*multiplier;
     if (hitPoints <= 0.0) {
         // Start emitting particles
         var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
         if (emitter)
             emitter.emit = true;
 
         Invoke("DelayedDetonate", effectDelay);
     }
 }
 
 function ApplyDamage (damage : float){
     //Info array contains damage and value of fromPlayer boolean (true if the player caused the damage)
     //Find the player if we haven't
     
     // We already have less than 0 hitpoints, maybe we got killed already?
     
     
     
     if (hitPoints <= 0.0){
         return;
         }
 
     //float.TryParse(Arr[0], tempFloat);
     hitPoints -= damage*multiplier;
     if (hitPoints <= 0.0) {
         // Start emitting particles
         var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
         if (emitter)
             emitter.emit = true;
 
         Invoke("DelayedDetonate", effectDelay);
         }
         
         if (hitPoints <= 0.0) {
             GameObject.Find("Player").GetComponent(ExperienceSystem).GiveXP(expVal);
             // Start emitting particles
         }
     
 
 }
 
 function Detonate(){
     if(isEnemy)
         EnemyMovement.enemies--;
     // Create the deathEffect
     if (deathEffect)
         Instantiate (deathEffect, transform.position, transform.rotation);
 
     // If we have a dead replacement then replace ourselves with it!
     if (deadReplacement) {
         var dead : Rigidbody = Instantiate(deadReplacement, transform.position, transform.rotation);
 
         // For better effect we assign the same velocity to the exploded gameObject
         dead.rigidbody.velocity = rigidbody.velocity;
         dead.angularVelocity = rigidbody.angularVelocity;
     }
     
     // If there is a particle emitter stop emitting and detach so it doesnt get destroyed right away
     var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
     if (emitter){
         emitter.emit = false;
         emitter.transform.parent = null;
     }
     BroadcastMessage ("Die", SendMessageOptions.DontRequireReceiver);
     Destroy(gameObject);
 
 }
 function DelayedDetonate(){
     BroadcastMessage ("Detonate");
 }

Okay so in the second ApplyDamage(); function I have an if statement that should send a value of 20 to my giveXP function for my player script. Here is that script.

 var guiTextEXP : GUIText;
 var accumulatedExperiencePoints : int = 0;
 var levelExpRequirements : int = 1000;
 var currentLevel : int = 1;
 var xp : int;
 
 function GiveXP(addedXP : int)
 {
     
     if (addedXP < 0)
     {
         Debug.Log("Can't remove exp!");
         return;
     }
 
     xp+=addedXP;
  
     if (accumulatedExperiencePoints >= levelExpRequirements)
     {
         levelExpRequirements += 1000;
         currentLevel += 1;
     }
     
 }

Now it am trying to get the first script to update the amount of accumulatedExperiencePoints that I have. But I am not sure how to do that. Can someone just point my in the right direction because I am clearly missing something lol. Thank you so much :)

Comment

People who like this

0 Show 5
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 LijuDeveloper · Nov 11, 2013 at 03:53 AM 0
Share
     public GameObject gameObject ; // In which attaching firstScript; 

     private firstScriptName firstScript;

     function Start() 

      {

        firstScript = gameObject.GetComponent<firstScriptName>();

      }

   function Update()

   {

      var secondVariable =   firstScript.variableName ;

   }


Try this idea

avatar image cwaite84 LijuDeveloper · Nov 11, 2013 at 05:56 AM 0
Share

I would be putting that in the enemy script?

avatar image LijuDeveloper LijuDeveloper · Nov 11, 2013 at 06:31 AM 0
Share

If you want to access variable from Player Script using Enemy script , then you should putting this code in Enemy Script . Else you want to access variable from enemy script using player script , then put in player script

avatar image cwaite84 LijuDeveloper · Nov 11, 2013 at 04:00 PM 0
Share

Yeah I had to first redesign that script so that it didn't throw me errors while still keeping the base idea, still no luck :/

avatar image cwaite84 · Nov 12, 2013 at 02:58 AM 0
Share

Still havent been able to figure it out.

0 Replies

· Add your reply
  • Sort: 

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

17 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

Related Questions

A node in a childnode? 1 Answer

Singletons in music script? 2 Answers

Script crashes Unity 1 Answer

CharacterSave Statistics 1 Answer

Experience System 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