Say I wanna get a reference to one of the player’s components, so I set player variable to public, and try to drag in player object so I can use it. But then write a simple code to check whether player is null or not, but the results as showed, it repeatedly show “Null” and “Not null”, which is the test code I just wrote, what’s the problem?
As Hellium said in the comment, I also think you may have attached this script multiple times, maybe to different objects or even the same. One has its player variable initialized, the other has not. You can add a “context” reference to each of your Debug.Log calls. That way you can click on the debug message in the console and Unity will ping / highlight that context object in the hierarchy. So you should be able to find the source for each of the calls.
if (player == null)
Debug.Log("Player is null", gameObject);
else
Debug.Log("Player is not null", gameObject);
Also you should get into the habit of using meaningful log messages so they can not be accidentally confused with a completely different one. I’ve seen people logging tons of integer and float values without any further information. So the log is a complete mess of numbers and you can not even tell which number means what. Debugging is a tool for you, the programmer. So use it wisely.
try using this
if(player != null)
{
Debug.Log("Not Null");
}
else
{
Debug.Log("Null");
}