- Home /
GameObject.FindObjectWithTag("GameManager") doesn't work [C#]
Hey everybody !
In the game that i'm creating, when the player prefab is instantciated, i use this command to find the game manager (switch from lobby to game scene). I have always the same issue : "Object reference do not set to an instance of an object".
This game use the Unity Lobby Network system.
Can some one help me ?
This is the script : (one part)
void Start () {
if (!isLocalPlayer)
{
PLAYER_Camera.enabled = false;
return;
}
if (GAMEMANAGER_GameObject == null)
GAMEMANAGER_GameObject = GameObject.FindWithTag("GameManager");
GAMEMANAGER_GameObject.GetComponent<GameManager_Survival>().SERVER_PlayerInstanceList.Add(this.gameObject);
Thanks you !
Have a good day,
xyHeat.
I'm glad you've found a reply that helps you. But it doesn't really answer your question, which is how this site is supposed to work.
The answer to the question is that
GameObject.FindWithTag does work, and
If GameObject.FindWithTag("Game$$anonymous$$anager") does not find anything, then you have no active GameObject with the tag "Game$$anonymous$$anager".
This site does exactly work they way people want it to work. If the thread creator is content with the provided solution because it thinks one step further, who are you to judge? Especially since the "right" answer from your perspective is even more harmful than the flawed singleton.
You'll find that it's not unusual on here for people to critique each other's questions and answers. But I'm sorry if my wording appeared somewhat fussy. I did not mean to offend and am not quite sure why I have (the meaning of your last sentence isn't at all clear to me).
$$anonymous$$y concern was just that other people would find this question, which appears to suggest that Find
doesn't work, and the accepted answer, which suggests an alternative to Find
without addressing what's going on in the original situation, and conclude that there is indeed something wrong with Find
. Plus, of course, whatever problems the OP may have with using Find
are unlikely to have been eased.
So it seemed to me that someone ought to at least address the question that was asked.
Answer by Raresh · Oct 17, 2016 at 05:01 PM
Why don't you write your GameManager using the singleton pattern? You will then basically call: GameManager.instance, and if there's a GameManager present, it will return you it's instance. No messy Find :)
Here's a tutorial: https://unity3d.com/learn/tutorials/projects/2d-roguelike-tutorial/writing-game-manager
Thanks you for your answer :).
I'm looking how to make a singleton, i hope that can help me :)
Bye, xyHeat
It's easy to understand from the video the concepts but basically:
You will have a static Game$$anonymous$$anager instance; in your Game$$anonymous$$anager.
In Awake(), if instance == null it means that basically this is the first time running, and it needs to be assigned. this Game$$anonymous$$anager instance will become the static variable
if instance != null it means we already have a Game$$anonymous$$anager assigned to instance (there are multiple Game$$anonymous$$anagers), and the current Game$$anonymous$$anager that isn't assigned to instance needs to be destroyed
Hope it makes some sense, more in the link
Your answer
![](https://koobas.hobune.stream/wayback/20220612094825im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GameObject.Find only working on server? 1 Answer
Using tags in edit and build 0 Answers
How to spawn gameobject from host to all clients 1 Answer
Network.Instantiate issues - GameObject ownership and network views? 0 Answers
UNet - How do I make Network.Spawn not show the prefab to the user that called it? 1 Answer