- Home /
[Resolved: Had to remove messenger listeners]When Instantiating a prefab, after a previous clone of said prefab has been destroyed, I get a "MissingReferenceException".
So I have a savefile menu, which has a UI Panel with this script (called "SavePanel") on it:
for(int i = 0; i < (GameController.current.saveFileNames.Count + 1); i++)
{
GameObject saveClone = Instantiate(saveFilePrefab);
saveClone.transform.SetParent(gameObject.transform, false); //helps to use this when dealing with UI objects, as it avoids scaling issues.
saveClone.GetComponent<SaveFileBtn>().slot = i;
Messenger.Broadcast(Broadcasters.SetupSaveFile.ToString());
}
inside the OnEnable() method. saveFilePrefab is a prefab which is basically a Button with this script (called "SaveFileBtn") on it:
public string saveFileName;
public int slot;
public bool isLoadBtn;
private Button btn;
private void Awake()
{
btn = GetComponent<Button>();
btn.onClick.AddListener(Click);
Messenger.AddListener(Broadcasters.SetupSaveFile.ToString(), Setup);
}
private void Click()
{
if(isLoadBtn)
{
Messenger<string, string>.Broadcast(Broadcasters.ShowLoadModalWindow.ToString(), "Are you sure you wish to load this file?", saveFileName);
}
else
{
if(saveFileName != null)
{
Messenger<string>.Broadcast(Broadcasters.ShowSaveNameEdit.ToString(), saveFileName);
}
}
}
public void Setup()
{
/* "slot + 1" because slot is zero-based and so if we did GameController.current.saveFileNames.Count >= slot, then this would
* be true even if GameController.current.saveFileNames.Count were zero.*/
if(GameController.current.saveFileNames.Count > slot)
{
saveFileName = GameController.current.saveFileNames[slot];
}
else
{
saveFileName = "New Save File";
}
if(gameObject == null)
{
Debug.Log("RIP svefilename");
}
else
{
GetComponentInChildren<Text>().text = saveFileName;
}
}
When I leave the savefile menu, I destroy the instantiated buttons. But if I go back INTO it, I get a MissingReferenceException:
MissingReferenceException: The object of type 'SaveFileBtn' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. Assets._Custom._Scripts._GUIScripts.SaveFileBtn.Setup () (at Assets/_Custom/_Scripts/_GUIScripts/Menus/SavingGUI/SaveFileBtn.cs:53) Assets._Custom._Scripts._Helpers.Messenger.Broadcast (System.String eventType, MessengerMode mode) (at Assets/_Custom/_Scripts/_Helpers/MessengerSystem/Messenger.cs:180) Assets._Custom._Scripts._Helpers.Messenger.Broadcast (System.String eventType) (at Assets/_Custom/_Scripts/_Helpers/MessengerSystem/Messenger.cs:165) Assets._Custom._Scripts._GUIScripts.SavePanel.OnEnable () (at Assets/_Custom/_Scripts/_GUIScripts/Menus/SavingGUI/SavePanel.cs:41) UnityEngine.GameObject:SetActive(Boolean) Assets._Custom._Scripts._GUIScripts.MainMenuState:ChangeState(State) (at Assets/_Custom/_Scripts/_GUIScripts/Menus/MainMenuState.cs:91) Assets._Custom._Scripts._Helpers.Messenger`1:Broadcast(String, State, MessengerMode) (at Assets/_Custom/_Scripts/_Helpers/MessengerSystem/Messenger.cs:223) Assets._Custom._Scripts._Helpers.Messenger`1:Broadcast(String, State) (at Assets/_Custom/_Scripts/_Helpers/MessengerSystem/Messenger.cs:209) Assets._Custom._Scripts._GUIScripts.MainMenuButton:Click() (at Assets/_Custom/_Scripts/_GUIScripts/Menus/MainMenuButton.cs:21) UnityEngine.EventSystems.EventSystem:Update().
Where line 53 of the SaveFileBtn script is the bit where it says if(gameObject == null). MainMenuButton is just the script which takes us into the savefile menu.
So, in summary, if I instantiate a prefab, destroy the clone, then try to re-instantiate the prefab, I get a MissingReferenceException.
Your answer
Follow this Question
Related Questions
Setting parent of instantiated object fails (Error: setting parent of prefab is disabled...) 1 Answer
Accessing a prefab after instantiating results as null 1 Answer
MissingReferenceException on other instances after destroying one. 1 Answer
can't reference an instance of an object (prefab) 1 Answer
Make runtime-instantiated game objects persistent OR make their unique ID persistent 0 Answers