Unity GetComponet<>().GetObject returns false with Equals

I have a class ProtagonistActor
Inside i have a public getter GetActorState.

So in a different class InputManager, I have:

ProtagonistActor _protagonistActor;
ActorState _actorState;

private void Awake()
{
    _protagonistActor = GetComponent<ProtagonistActor>();
    _actorState = _protagonistActor._actorState;
}


void OnJumpEvent(){
    Debug.Log($"Comparingggggggggg {_protagonistActor._actorState.Equals(_actorState)} {_protagonistActor._actorState == _actorState}");

}

I was expecting Log to print True but it is printing False for both. How is this possible?

Does GetComponent().GetSome…Getters returns a new reference?;

I event printed out hashcode, they are giving different values, How?

Comparingggggggggg False False 1301530776 -256054432
1 Like

I would also guess that it’s a race condition issue since you made the classical mistake to rely on data of other components in Awake. Awake should be used to initialize yourself, Start should be used to reach out to other components data. You can setup reference to other objects in Awake, but don’t expect them to have initialized themselfs yet. What exactly is stored in “_actorState” and who and when is that variable initialized? Your awake method probably runs before that.

1 Like

Thank you, it was a race condition

1 Like