Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by RobobertSmitty · Dec 13, 2016 at 05:27 PM · javascriptvariablesglobal script

Changing a Variable from another script. JavaScript

So I'm trying to get a player to level up with XP. I have the xp needed and scaling in one script and detecting if a monster is killed as well. On the Monster script I have the damage the player does as well as sending if the monster is killed. I'm still new to this and looking online has not helped. I think the static variable only makes a copy in the second script and I am unable to change the value from the original script. Please tell me why it is not working and what is going on instead and how to fix it.

Script one with the set variables to be sent and later changed. public static var Enemy1 :boolean=true; public static var DamageX : float=1; public static var exp=1; public static var level=1f; public static var levelexp=0; public static var expneeded=2f; public static var DamageXL: float=DamageX; public static var expL=exp; public static var levelL=level; public static var levelexpL=levelexp; public static var expneededL=expneeded; public static var j= Mathf.Round(expneededL); public static var EM1=Enemy1;

         function  Update () {
             exp=expL;
             level=levelL;
             j= Mathf.Round(expneededL);
             DamageX= DamageXL;
                       if (EM1==true){
                                      Debug.Log(Enemy1);
                                      expL+=10;
                                      Enemy1=false;
                                      }
                       else {Enemy1=false;}
                       if (expL>=expneededL){
                                      expL=expL-expneededL;
                                      levelL=levelL+1;
                                      expneededL=6/5*levelL*levelL*levelL-15*levelL*levelL+100*levelL-140;
                                      DamageXL=1+(levelL/4);
                                             }
    
                             }
 I have a copy of each static made so I could see live updates in the game since the static variable could not be manipulated mid scene to me.

Here is the second script (parts that do not work)

 public var damagelocal=Global.DamageX;
 
 function OnTriggerEnter2D(obj) {
     // Name of the object that collided with the enemy
     var name = obj.gameObject.name;
     var r2d = GetComponent("Rigidbody2D");
     if (name == "Enemy") {
         r2d.transform.position.y=r2d.transform.position.y;
         r2d.transform.position.x=r2d.transform.position.x;}
     if (name == "Player"){
         r2d.transform.position.y=r2d.transform.position.y;
         r2d.transform.position.x=r2d.transform.position.x;}
     // If the enemy collided with a bullet
     if (name == "ball 1(Clone)") {
         health=health-1;
         Destroy(obj.gameObject);
         if (health<=0){
             // Destroy itself (the enemy) and the bullet
             Global.Enemy1=true;
             Destroy(gameObject);
             Destroy(obj.gameObject);}
     }
 
         // If the enemy collided with the spaceship
     else if (name == "Slash Top(Clone)" ){
         health=health-damagelocal;
         Destroy(obj.gameObject);
         if (health<=0){
             // Destroy itself (the enemy) and the bullet
             Destroy(gameObject);
             Destroy(obj.gameObject);
             Debug.Log("enemy killed by slash");
             }
     }}
 The enemy tracks the player and stops if it hits a player or another enemy. If the enemy is destroyed it is suppose to update the boolean but it seems to update it only inside of this script. And changing the value manually seems to have no effect either so I am at a loss.



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

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.

Follow this Question

Answers Answers and Comments

113 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help with variables 1 Answer

How can I make A Object have the same Scale as B Object? 0 Answers

compare variables JS 1 Answer

Why is my script only running when object has been selected beforehand? 1 Answer

Parameter for animation not updating 0 Answers


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