GetComponent() doesn't work in Build (works in Editor)
Strange issue. Simple GetComponent() code works well in editor but simply returns null in build.
Code is executed when a player object gets instantiated into the game scene in Start():
GM =GameObject.FindWithTag("GameMaster").GetComponent<Game_Master>();
Debug.Log("" + GM.gameObject.name);
Control_Manager = GM.GetComponent<Player_Control_Manager>();
Debug.Log("" + Control_Manager.gameObject.name);
In theory this script should print the same name in both Debug.Log(). In Editor everything works. In build - the second Debug.Log() always produces null error.
Answer by Bunny83 · Jan 12, 2020 at 06:41 PM
Well, if GetComponent returns null, there is no component of that type on the gameobject. It's as simple as that. When testing in the editor, GetComponent always returns an instance. However in case no component is found a fake null object is returned. This is not the case at runtime. Though it doesn't change your actual issues that there is no "Player_Control_Manager" component on the gameobject with the tag "GameMaster".
Your answer
Follow this Question
Related Questions
IOS build with Linux Editor non responsive 0 Answers
Cannot open build on Ubuntu 0 Answers
Can't Build with IL2CPP for Android after 2018.3.0 Update(Empty Project) 1 Answer
Prefabs won't appear in builds. 0 Answers
UWP Build issue 0 Answers