- Home /
why does the script disables itself when referencing something in Awake?
I want my singleton GameManager (that won't be destroyed on load) to keep track of the active player in the scene. I try to do this by adding this simple script to the player. However it is disabling itself or becoming gray in play mode. This is not the case when I put the line of code in Start. Do u maybe have any recommendations of what the best (reliable) way is to always keep track of the active player so that when the player switches between characters the camera etc. is updated, should I use delegates or events for that?
public class PlayerTest : MonoBehaviour
{
private void Awake()
{
PlayerManager.instance.currentPlayer = this.gameObject;//this gives problem
}
}public class PlayerManager : MonoBehaviour
{
public GameObject currentPlayer;
#region Singleton
public static PlayerManager instance;
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}.
//etc
Answer by wyatts · Aug 27, 2019 at 07:28 AM
What's wrong with assigning the current player in "Start"? Awake loads before start -- so you'd want your singleton to establish itself in Awake before you assign the player instance in Start. This is the purpose of Awake & Start functions.
Your answer
![](https://koobas.hobune.stream/wayback/20220612211241im_/https://answers.unity.com/themes/thub/images/avi.jpg)