- Home /
Script work in editor but not in build
Hi, i have problem with my script. It's working in Editor but when i restart Unity or build game it stops working (like it's doesn't exist).
Here's my script:
using UnityEngine;
using System.Collections;
public class Leveling : MonoBehaviour {
public Texture2D Exp;
public Texture2D Background;
public int actLevel = 0;
public int actExp = 0;
public int[] expToNextLevel;
public string[] addons;
public GUIStyle style;
public void gainExp(int exp)
{
actExp += exp;
}
IEnumerator Wait(float time)
{
yield return new WaitForSeconds(time);
}
void Update()
{
if (actLevel >= expToNextLevel.Length - 2)
{
actLevel = expToNextLevel.Length - 2;
}
if (actLevel >= expToNextLevel.Length - 1)
{
actExp = 0;
}
if (actExp >= expToNextLevel[actLevel + 1])
{
actLevel += 1;
actExp = 0;
}
}
void OnGUI()
{
float exPerc = 0;
exPerc = (float.Parse("" + actExp) / float.Parse("" + expToNextLevel[actLevel + 1])) * 100;
GUI.DrawTexture(new Rect(5, Screen.height - 25, 100, 10), Background);
GUI.BeginGroup(new Rect(5, Screen.height - 25, exPerc, 10));
GUI.DrawTexture(new Rect(0, 0, 200, 10), Exp);
GUI.EndGroup();
GUI.Label(new Rect(105, Screen.height - 30, 1000, 1000), "" + actLevel, style);
}
}
What do you mean by "not working" do you mean showing no effect or failing to build or crashing, the only thing I can think that could be going wrong is a null reference exception with the Texture2D's
On second thought, where is gainExp(int exp)
being called, it isn't public so it isn't being called by another script
A more general comment on debugging code that doesn't work outside the editor, you can attach monodevelop to the running unity process and use step-through debugging, breakpoints etc, which makes it very easy to see why your code is 'not working'.
http://answers.unity3d.com/questions/7572/how-can-i-attach-a-debugger-to-the-code-running-on.html
In what way is your script "not working". Please be more specific and reword question title to a) help people answer more effectively, and b) make this question of greater use for future readers :)
I am having a similar problem with key inputs not being recognized outside the editor. Please post an answer if you figure this out...
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Custom Timeline Playable - GUI Override 0 Answers
How to add a reorderable list on CUSTOM EDITOR WINDOW? 0 Answers
UI vs Scripts question - what's where? 3 Answers
Problems with instantiation... 1 Answer