0

After a couple of hours of tackling a nasty bug, I managed to break it all down to one single thing that I can't comprehend.

I have a gameObject character, which has 2 component scripts.

1) StateSwitcher (a class applying the factory design principle: it returns states depending on certain conditions of the character)

2) General State (a class from which all states inherit, which contains general state data and behaviour

My character inspector looks like this: enter image description here

As you can see, I assigned the StateSwitcher to GeneralState by simply selecting it in the inspector view.

This is what GeneralState.cs look like:

// This class contains all fields, properties & methods shared across all states
public class GeneralState : MonoBehaviour
{
    private void Start()
    {
        Debug.Log(StateSwitcher);
    }
}

This is what baffles me:

enter image description here

edit 1:

// This class contains all fields, properties & methods shared across all states
public class GeneralState : MonoBehaviour
{
    private void Start()
    {
        Debug.Log(StateSwitcher.GetInstanceID());
    }
}

Results in this:

enter image description here

edit 2:

// This class contains all fields, properties & methods shared across all states
public class GeneralState : MonoBehaviour
{
    private void Start()
    {
        Debug.Log(this.GetInstanceID());
    }
}

results in this:

enter image description here

Thrindil
  • 143
  • 3
  • 17
  • 1
    Are u sure it is the only one instance of GeneralState? Try to replace your Debug.Log with Debug.Log(this.GetInstanceID()) and compare results. – Smith Jan 12 '19 at 10:52
  • 1
    You're printing StateSwitcher.GetInstanceID() instead of this.GetInstanceID(). We want to compare GeneralStates ids to check if there is only once instance of it. – Smith Jan 12 '19 at 11:05
  • 3
    Now i'm sure. You have 2 instances of GeneralState. Try to find it by writting "t:GeneralState" in search bar. – Smith Jan 12 '19 at 11:14
  • @Wuszt I found it! It was an object called "FreeWalkStateObject" with a FreewalkState script attached to it. And FreeWalkState inherits from GeneralState, so it was hiding in plain sight. I'm having a slightly different issue now though: that FreeWalkState script was needed in my scene, because my StateSwitcher script had a reference to it. My StateSwitcher basically creates different States, so it needed a reference to FreeWalkState (to create it). But how can I assign FreeWalkState to StateSwitcher, without having it attached to a gameobject in my scene? – Thrindil Jan 12 '19 at 11:54
  • 1
    `I'm having a slightly different issue now though` https://meta.stackexchange.com/questions/39223/one-post-with-multiple-questions-or-multiple-posts – mjwills Jan 12 '19 at 12:23
  • A good rule-of-thumb for debug logging is to prefix the output with the gameobject name, eg `Debug.Log( gameObject.name + " ClassName::Function():" + message);` – Immersive Jan 14 '19 at 06:01
  • @Immersive Thanks, I'll keep that one in mind! – Thrindil Jan 14 '19 at 08:10

0 Answers0