Forum rules - please read before posting.

Get Player problem

Hey Chris,

I am making a Customization menu and i am using UnityUI prefab, i have a custom script at the prefab that gets the current player with
player = AC.KickStarter.player.gameObject;
or with
player = FindObjectOfType().gameObject; (Which is a script with info on each Player)

I am calling this funtion from a custom Action List which is being called after I switch the Player but the result is a missing GameObject even though i print the proper player.

If I call the same function in update of the Menu prefab then i am getting the proper player each time.

Is there a delay from switching to one player to another and the call of my action that could result to a missing gameobject maybe...?

https://imgur.com/a/49sQE3q

Comments

  • Try inserting an Engine: Wait Action of -1 second to delay the next Action by a single frame.

    However, the best way to react to a change in Player character is to rely on the OnSetPlayer custom event. This event will be fired once a new Player has been spawned and set up, and also passes the new Player as a parameter, i.e.:

    private void OnEnable () { EventManager.OnSetPlayer += OnSetPlayer; }
    private void OnDisable () { EventManager.OnSetPlayer -= OnSetPlayer; }
    
    private void OnSetPlayer (Player player)
    {
        //
    }
    

    This would be placed in the scene inside a regular MonoBehaviour script, rather than a custom Action.

  • Thanks Chris, it works better now.

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.