Forum rules - please read before posting.

Bug with isActivePlayer?

I am trying to make a simple set of character switching buttons that will hide themselves when the character they are tied to is the active player (i.e., the character the player currently controls). I wanted to use the AC.Character.isActivePlayer()

I'm trying to achieve this in code with the following:

// The character here is set in the inspector to be the prefab set in the DEFAULT characters slot
    [SerializeField]
    private AC.Player mainCharacter;
...

    void Update()
    {
        bool isActive = mainCharacter.IsActivePlayer();
        Debug.Log("is active? :" + isActive);
    }

Upon running the game, the main character spawns and is controllable as expected, but the Debug Logs is active? : False. I checked the Object where this functionality is instanced, and the variables are set correctly. Am I using this function incorrectly or is this an issue?

Comments

  • edited March 30

    Welcome to the community, @DankShellz.

    IsActivePlayer is the correct function - but I suspect it's being called on the wrong object.

    When Unity spawns a prefab into the scene, this copy is technically a separate object. It's the scene instance - not the original prefab - that becomes the active Player.

    If you want your Inspector to reference the original Player prefab as it is now, you can iterate through the Settings Manager's players List, find the one that references the original prefab, and then use GetSceneInstance to get the copy that's in the scene.

    Try adding this to your script:

    private Player mainCharacterInstance;
    private bool IsActivePlayer ()
    {
        if (mainCharacterInstance == null)
        {
            foreach (var playerPrefab in KickStarter.settingsManager.players)
            {
                if (playerPrefab.EditorPrefab == mainCharacter)
                {
                    mainCharacterInstance = playerPrefab.GetSceneInstance ();
                    break;
                }
            }
        }
        return mainCharacterInstance.IsActivePlayer ();
    }
    

    And then replace:

    bool isActive = mainCharacter.IsActivePlayer();
    

    with:

    bool isActive = IsActivePlayer();
    
  • Ah right you are, the prefab instance was technically a different object from the prefab I had assigned in the inspector. Thanks!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.