The struggle

Today something about  why this happened:


Maybe something about operation execution order in Unity too.

First of all, let’s look at piece of code

public static void InitializeNeeds(PillAi pill)
    var gc = GameObject.FindObjectOfType<GameController>();
    if (pill.GetNeedsDictionary() == null)
        pill.SetNeedsDictionary(new Dictionary<NeedType, double>());
    foreach (var key in gc.DefaultNeedVaules.Keys)
        pill.AppendNewNeed(new KeyValuePair<NeedType, double>(key, gc.DefaultNeedVaules[key]));

What you can see is static method that defines needs for every object on scene. It should do the job. But what I get after runing the game is…


Not the greatest result, isn’t it? But thankfully, Visual Studio has some cool debugging support for Unity, so let’s find out what went wrong.

And because it will be important later, this is GameController’s Start method

        private void Start()
            DefaultNeedVaules = SetDefaultNeedValues();
            Pills = FindObjectsOfType<PillAi>().ToList();

Again – DefaultNeedValues should be initialized first in order for InitializeNeeds method to work. What actually happens is InitializeNeeds being executed first. That means, my approach being wrong.

But that is actually okay. Default need values were one of ugliest examples of hardcoding I have made, and believe me –  I have made some.

I am thinking of keeping them in some kind of XML. So my next post will touch the subject of XML parsing in Unity.

The struggle


Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:


Komentujesz korzystając z konta Wyloguj / Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Wyloguj / Zmień )

Zdjęcie na Facebooku

Komentujesz korzystając z konta Facebook. Wyloguj / Zmień )

Zdjęcie na Google+

Komentujesz korzystając z konta Google+. Wyloguj / Zmień )

Connecting to %s