In my 2D project there is some UI and clicking on some buttons retrieves objects from a Firebase database and for example display them by filling a parent ui object. When I click on these buttons from the editor’s play mode, it crashes with the message :
Microsoft Visual C++ Runtime Library
Program:
This application has requested the Runtime to terminate it in an unusual way. Please contact the application’s support team for more information.
However, when I build the project, and play the game from the executable, I can click on the buttons properly without any crash.
The issue is that I have no clue why it does that, it worked pretty fine before and I haven’t changed anything to the fetching or displaying logic. If only it gave some info on the crash, but my Editor.log file doesn’t give any info.
Here is an example of code that provokes the crash.
This method is assigned to a button :
public void CommunityMenu()
{
presetsMenu.SetActive(false);
myLevelsMenu.SetActive(false);
communityMenu.SetActive(true);
foreach(Transform child in communityContent.transform)
{
Destroy(child.gameObject);
}
levels.Clear();
StartCoroutine(DatabaseManager.instance.GetAllLevels((userLevels) =>
{
foreach(UserLevel userLevel in userLevels)
{
GameObject newCommunityItem = Instantiate(communityItem, communityContent.transform);
newCommunityItem.GetComponent<Button>().onClick.AddListener(() => OnCommunityButtonClick(userLevel.level, userLevel.levelName, userLevel.levelImage, userLevel.level.obstacles.Length));
newCommunityItem.transform.GetChild(0).GetComponent<TMP_Text>().text = userLevel.levelName;
newCommunityItem.transform.GetChild(1).GetComponent<TMP_Text>().text = "by " + userLevel.userName;
newCommunityItem.GetComponent<LevelValues>().level.publicationDate = userLevel.level.publicationDate;
newCommunityItem.GetComponent<LevelValues>().levelName = userLevel.levelName;
newCommunityItem.GetComponent<LevelValues>().userName = userLevel.userName;
newCommunityItem.GetComponent<LevelValues>().image = userLevel.levelImage;
StartCoroutine(DatabaseManager.instance.GetRatingForLevel(userLevel.levelName, "QualityRatings", (average) =>
{
newCommunityItem.GetComponent<LevelValues>().averageQualityRating = average;
}));
StartCoroutine(DatabaseManager.instance.GetRatingForLevel(userLevel.levelName, "DifficultyRatings", (average) =>
{
newCommunityItem.GetComponent<LevelValues>().averageDifficultyRating = average;
}));
levels.Add(newCommunityItem);
}
OnCommunityButtonClick(userLevels[0].level, userLevels[0].levelName, userLevels[0].levelImage, userLevels[0].level.obstacles.Length);
}));
}
And the button that is instantiated has this onclick listener :
public void OnCommunityButtonClick(Level level, string name, Sprite levelImage, int obstaclesCount)
{
LevelValues levelValues = levelDetailsPanel.GetComponent<LevelValues>();
TMP_Text levelName = levelDetailsPanel.transform.GetChild(10).GetComponent<TMP_Text>();
levelName.text = name;
Image image = levelDetailsPanel.transform.GetChild(0).GetComponent<Image>();
image.sprite = levelImage;
TMP_Text obstaclesCountText = levelDetailsPanel.transform.GetChild(3).GetComponent<TMP_Text>();
obstaclesCountText.text = obstaclesCount.ToString() + " obstacles";
TMP_Text descriptionText = levelDetailsPanel.transform.GetChild(2).GetComponent<TMP_Text>();
if (level.description != null)
{
descriptionText.text = level.description;
} else
{
descriptionText.text = "no description";
}
TMP_Text publicationDateText = levelDetailsPanel.transform.GetChild(11).GetComponent<TMP_Text>();
if(level.publicationDate != null)
{
publicationDateText.text = level.publicationDate;
} else
{
publicationDateText.text = "";
}
Button playButton = levelDetailsPanel.GetComponentInChildren<Button>();
playButton.onClick.AddListener(() => OnCommunityLevelPlayClick(level, name));
levelValues.levelName = name;
StarRating qualityRating = levelDetailsPanel.transform.GetChild(7).GetComponent<StarRating>();
StartCoroutine(DatabaseManager.instance.GetRatingForLevel(name, qualityRating.gameObject.name, (value) =>
{
qualityRating.currentRating = value;
qualityRating.UpdateStars(value);
}));
StarRating difficultyRating = levelDetailsPanel.transform.GetChild(4).GetComponent<StarRating>();
StartCoroutine(DatabaseManager.instance.GetRatingForLevel(name, difficultyRating.gameObject.name, (value) =>
{
difficultyRating.currentRating = value;
difficultyRating.UpdateStars(value);
}));
}
Any idea about what may cause this crash ?
Thank you for your time and attention