Unity4 Ignoring Inspector/Initialized Public Value in Build, Works Editor UnityV3

This is a continuing effort to solve the issue I discovered in my movement file, 1st reported here:
Unity Support Forums
http://forum.unity3d.com/threads/166392-CharacterController.Move-not-responding-in-Build-Works-Fine-in-Editor

It is not a controller.move bug as I earlier suspected. See below.

I currently use a controller.move call, multiplied by a moveSpeed float and Time.Deltatime. This call works perfectly in Unity 3.5.6 and all earlier versions I utilized (in both editor and build) Unity 4 Editor Game Window. However, as you can see below: the problems begin when I build my project.

I originally had moveSpeed initialized at 5f, and that would be replaced by the public inspector value (as different entities move at different speeds). Unfortunately, Unity4 builds ignore moveSpeed initialization and public inspector value entirely. There is no where in this code where the value is every set (it is only multiplied against values like sprintMultiplier). Additionally, the only workaround I have found is to ‘hard-code’ the moveSpeed to 5 in the Actions call.

The real problem with nailing down this problem, is that it DOES NOT OCCUR IN THE EDITOR. If you notice, I had to use the game-console window to output Debug.Log statements because I cannot recreate this bug in Unity4 builds.

If anyone has a solution or insight on what I might be doing wrong, or if this is a known error; I would really appreciate it, as this is single handedly keeping me from updating to Unity 4 atm. (well, maybe not by itself; there’s some really crappy frame rates associated with OnTriggerEnter…but that’s a whole other beast.

What’s the declaration of moveSpeed look like?

Update: It’s simply not getting set. I thought perhaps something in my code was setting it to 0 every frame, but if I hardcode the moveSpeed in the Start() or anywhere else, the value persists.

I’m using pretty typical code to setup my public variable:

public float moveSpeed ; // Base movespeed for all players and targets
public float sprintMultiplier = 2; // Multiplier applied to moveSpeed during Sprint/Dash
private float gravity = 50; // Rate jumping/falling players are pulled toward ground
public float airTime = 0; // Time player has spent in air since last time we touched ground
private float fallTime = 2.0f;

I then set the moveSpeed in the inspector. In the editor, the value persists, but in the build, the moveSpeed doesn’t initialize. I have also tried to initialize the value with:

public float moveSpeed = 5f ;

and

public float moveSpeed = 5 ;

Neither work in the build. Movespeed remains at 0 unless I hard code it (which I could find a work around for, but honestly; I don’t want to have to worry about all my public variables set in inspector stubbornly being null in the build).

Edit: As far as I can tell, the other floats are working fine as I was able to test gravity by moving the spawn way up in the air. AirTime and Jumping variables are ok too; I was able to test them when I hardcoded moveSpeed.

Changing the variable to include an initial value like that won’t do anything if the variable is not new; all previous inspector values are kept.

That it’s just this one variable and not all of them makes me extremely suspicious; it would have to be an extremely strange bug in Unity that would affect only this variable and not everything else. What’s most strange is that it works in the editor…

More code is required, I think. Can you show us the entire source for the component, or at least the function where you call CharacterController.Move(), where you Debug.Log the value out, and where you’ve tried hardcoding it?

I have also been experiencing something like this. All of the public variables in one of my classes seem to get set to 0 when run in the standalone player.

Field initializers are implemented as constructor logic.

http://docs.unity3d.com/Documentation/ScriptReference/index.Writing_Scripts_in_Csharp_26_Boo.html

I have reported my issue as a bug. Unity QA have recreated it and are dealing with it :slight_smile:

This is a realy big problem, or is there any solution for this?

I had ThirdPersonCamera.cs and ThirdPersonCamera.js, removing one of them solved my problem.

^

You just saved my life. :slight_smile: Making a very huge fps and this bug happened when I built to Windows. I was pulling my hair! My character just jammed, no gravity, no movement, no error.

Hi
After the last update, the Inspector appears to say “object game” and I can not drag anything there.
But the code runs fine and is assigned values​​. But now I can not change anything!
I need help

Today is December 8, 2014, using Unity 4 v4.5.5f1 and MonoDevelop 4.0.1.

I just had this bug while writing a very simple C# script in Unity:

I added a JS script component to a Sphere via the Add Component button, then deleted it from my assets (I wanted a C# script instead), created a new C# script in the assets, coded stuff in MonoDevelop then drag ‘n’ dropped it on the “missing (Mono Script)” message (because I deleted the JS script). I then noticed my speed value was not showing up correctly in the Inspector; kept being 0.

public float speed = 2f;

After finding this thread and Zerano’s reply, I completely removed the script component and re-attached my C# script; problem solved…

To replicate
Edit your code in MonoDevelop, save, then go in Unity: public variable initialization value isn’t updated. Not in the editor, nor at run time.

Also discovered MonoDevelop would “break” if I edit a JS script attached to a GameObject that also has a C# script at the same time: all MonoDevelop’s menu/texts becomes squares (like missing fonts). :smile:

For knowledge’s sake, I’m posting my findings here.

Again, thank you you @Zerano for the tip on the .js and .cs scripts. My Unity must’ve somehow kept in memory that my script was still a .js, though I deleted it.

Sirus