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 Pua Kai · Apr 07, 2013 at 03:14 PM · editorbuilddifferent

Script works differently in build

Hi. I created a game for fun, and this script (UnityScript) works well in the editor, but after i compile it, be it exe or webplayer, the script only runs well for the first scene that is loaded. after that, the script work strangely in following scenes. In the script i've attached below, the testing command in "function lifed (val : float){...}" when i press "j" only works for the first frame i press it, after that it is ignored completely. Life remains 99. Need help!

 var life = 100.0;
 var power = 100.0;
 var lifebar : GameObject;
 var powerbar : GameObject;
 var health_power : health_power;
 var movement : movement;
 var jump_and_fly2 : jump_and_fly2;
 var healthsave : GameObject;
 private var died = false;
 
 //life bar range = xpos 0.82~1.11
 // power bar range = xpos 0.82~1.05
 function Start () {
     healthsave = GameObject.Find("save health and power");
     health_power = healthsave.GetComponent("health_power");
     lifebar = GameObject.Find("Health bar life");
     powerbar = GameObject.Find("Health bar power");
     movement = gameObject.GetComponent("movement");
     jump_and_fly2 = gameObject.GetComponent("jump_and_fly2");
 }
 
 /*
 test (put in Update().
     if (Input.GetKey("h")){
         powered(-1);
     }
     else powered(1);
     
     if (Input.GetKey("j")){
         lifed(-1);
     }
     else lifed(1);
 */
 
 function Update () {
 if (!died){
     powered (0.5*Time.deltaTime);
     lifed (0.05*Time.deltaTime);
     if (Time.timeSinceLevelLoad>Time.deltaTime){
         health_power.health = life;
         health_power.power = power;
 //        if (life<0.1){
 //            movement.enabled = false;
 //            jump_and_fly2.enabled = false;
 //            health_power.died = true;
 //            dying();
 //            animation.CrossFade("die");
 //        }
     }
     if (Input.GetKey("j")){
         lifed(-1);
         powered (-1);
     }
 }
 }
 
 //function dying () {
 //    yield WaitForSeconds(5);
 //    Application.LoadLevel(1);
 //}
 
 function powered (val : float){
 if (!died){
     if (val<0 && power>0){
         powerbar.transform.Translate(-0.0023*val, 0, 0);
         power += val;
         if (powerbar.transform.position.x > 1.05){
             powerbar.transform.position.x = 1.05;
             print ("no power");
         }
         if (power<0){
             power = 0;
         }
     }
     else if (val>0 && power <100){
         powerbar.transform.Translate(-0.0023*val, 0, 0);
         power += val;
         if (powerbar.transform.position.x < 0.82){
             powerbar.transform.position.x = 0.82;
         }
         if (power>100){
             power = 100;
         }
     }
 }
 }
 
 function lifed (val : float){
     if (val<0 && life>0){
         lifebar.transform.Translate(-0.0029*val, 0, 0);
         life += val;
         if (life<0){
             life = 0;
             died = true;
         }
         if (lifebar.transform.position.x > 1.11){
             lifebar.transform.position.x = 1.11;
             dead();
         }
     }
     else if (val>0 && life<100){
         lifebar.transform.Translate(-0.0029*val, 0, 0);
         life += val;
         if (lifebar.transform.position.x < 0.82){
             lifebar.transform.position.x = 0.82;
         }
         if (life>100){
             life = 100;
         }
     }
 }
 
 function OnTriggerEnter (hitInfo : Collider){
     var attackplayer : attackplayer = hitInfo.gameObject.GetComponent("attackplayer");
     if (attackplayer != null){
         yield WaitForSeconds(Time.deltaTime);
         lifed(-attackplayer.damage);
     }
 }
 
 function OnTriggerStay (val : Collider){
     if (val.gameObject.tag == "fire"){
         lifed(-10*Time.deltaTime);
     }
 }
 
 // ***************************** death ************************************
 
 var deadscr : dead;
 var pausescr : pausemenu;
 var miscscr : misceffects;
 var movescr : movement;
 var jumpscr : jump_and_fly2;
 var weapscr : weapon;
 
 function dead () {
     miscscr.enabled = false;
     pausescr.enabled = false;
     weapscr.enabled = false;
     deadscr.dead = true;
     movescr.dead = true;
     jumpscr.dead = true;
 }

I'm sorry if it was too jumbled up. I am a beginner in programming myself so... :S PS this script's gameobject is not destroyed during loading.

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 Pua Kai · Apr 07, 2013 at 11:50 PM 0
Share

ok, i resolved the problem, but it is still strange. i have to call the start function for everytime the scene is loaded, but the gameobjects i called in Start() are not destroyed on load too, why is the function needed every scene?

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

10 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

Related Questions

Distribute terrain in zones 3 Answers

Build and Editor collisions behaving different? 0 Answers

Setting different values on editor and build 1 Answer

Script works in editor, but does not work in build (Android) 1 Answer

How do I extract the Unity version used to build 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