- Home /
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.
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?