Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How do you test a method before using?

Discussion in 'Scripting' started by DarkCSFixer, Oct 7, 2017.

  1. DarkCSFixer

    DarkCSFixer

    Joined:
    Feb 24, 2017
    Posts:
    19
    I ran into a small problem with loading my game. If one of the items (Int, strings, bool) is not found while deserializing the save data it comes back with an error and stops. This isn't too big of a problem since the only reason it's not finding the items is because I'm editing the game data, but I want to test the method before it starts. That way if it does come back with an error it simply doesn't load it and starts a new save.

    So in other works how do you test a method before invoking it?

    Code (CSharp):
    1. if (File.Exists(Application.dataPath + "/SAVE/savedata.moewars"))
    2.         {
    3.             SaveData.SaveManager.LoadGame();
    4.             TimeManager.TimeUpdate();
    5.         }
    I want it to check if LoadGame() comes back with an error or not.
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
  3. DarkCSFixer

    DarkCSFixer

    Joined:
    Feb 24, 2017
    Posts:
    19
    Thanks that helped a lot :D
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Just thought I'd add another option: If it's not an exception for the method to fail, you can always return a value from the method indicating it's not ready or failed or what not. Maybe this would be returning an integer, for example, or a bool. :)