Runs fine in editor, tons of exceptions in build

I’ve built a game that runs fine in the editor. But once I play it in build (developer mode on), exceptions pop up.

By reading the output_log.txt in xxx_Data, I have tracked down the problem to my Dictionary for pool manager. I was putting pools into a pool manager for easy reference later.

The code looks something like this on the Pool.

[SerializeField]
String _name;
void Awake() {
    Debug.Log("Name: "+_name);
    PoolManager.AddPool(_name, this);
}

The _name is a unique identifier for the pool. For example, “Projectiles”, “Debris” etc. Now here comes the fun part.

In the editor, the console displays:

Name: Debris
UnityEngine.Debug:Log(Object)
Name: Projectiles
UnityEngine.Debug:Log(Object)

In other words, absolutely no problem.

But in my build, the file logs the following:

...
UnloadTime: 0.349474 ms
Name: [SOH] // <------------------------ HERE!!!!
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
Vst.Performance.PoolBase:Awake()
...

[SOH] is an ASCII character. Like [NUL] and other non-displayable characters. You’ll know if you’ve used Notepad++ before.

So my build is actually, using [SOH] as a key to the Dictionary. What follows are catastrophic. When my Projectiles pool gets added, because Debris is already inside the dictionary with key [SOH], I first get ArgumentException, saying the key already exists.

Later, when my weapons start instantiating projectiles from the pool, I get TONS and TONS of KeyNotFoundException because the pool couldn’t be added in the first place.

Since the Awake and Start of said objects are cut off midstream due to exceptions, they fail to instantiate properly, leading to TONS and TONS of NullReferenceException when the Update loop starts to run.

So can anyone tell me WHY this is happening to me?

A million thanks.

Vst.

The only thing I can think of is that something is called in a different order when running trough the build. Maybe it’s just been luck that _name has been set in the editor so far.

Take a look at where you set _name and see if it’s called before you use _name.

It seems that I have discovered the reason why such tragedy happens.

Consider the following code, put in ONE file named TesterBase.cs.

namespace SomeNamespace {

    public class TesterBase : MonoBehaviour { // provides access to raw type Tester<T>
        
        [SerializeField]
        int _number = 100; // just a simple number
        
        public int Number { get { return _number; } }
        
    }
    
    public class TesterBase<T> : TesterBase where T : class { // the class that will be inherited
        
        [SerializeField]
        T[] _objects;
        
        public T[] Objects { get { return _objects; } }
        
    }
    
}

And in another file, namely SomeObject.cs:

namespace SomeNamespace {

    public class SomeObject : TesterBase<String> {
        
        void Update() {
            Debug.Log("number: "+Number);
            Debug.Log("length: "+Objects.Length);
        }
        
    }

}

This will work perfectly in the editor. I set _number to 10 and put hello and world as two strings in the Objects array. When played, the log repeatedly prints out:

number: 10
length: 2

So we are cool.

BUT in the build, the log first shows some weird errors saying:

The file 'C:/.../xxx_Data/mainData' is corrupted! Remove it and launch unity again!
[Position out of bounds! 1819058236 > 15076]

Sometimes the Objects array can be instantiated properly and does not return null. But the length is definitely wrong (5 is printed in my case). For _number, the value can be quite random (I got 2).

TO FIX IT, the non-generic base class and generic base class (TesterBase and TesterBase respectively) should not share the same name.