Search Unity

Is there an easy way to output a message if Resources.Load fails to find a resource?

Discussion in 'Scripting' started by kheldorin, Sep 2, 2015.

  1. kheldorin

    kheldorin

    Joined:
    May 28, 2015
    Posts:
    22
    Just for debugging. I don't really want to insert null checks everywhere or wrap the Resources.Load function if there's already an in-built method that I'm missing.
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Check if the result is null. Pretty sure that's it. I guess you could try the asset pipeline but you would still be checking if something exists do I don't understand the aversion to null.
     
  3. kheldorin

    kheldorin

    Joined:
    May 28, 2015
    Posts:
    22
    It makes the code messy.It just helps if there's some sort of flag like a preprocessor that if enabled, unity would output in the console if it couldn't load a resource.

    For example, if I'm loading in a lot of different resources based on an excel sheet, and I'm trying to track down a typo, it'd be great not having to insert if null Console.log everywhere.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Returning null if the resource you are looking for is null kind of makes sense...

    But if null checks bother you, simply wrap the call to Resource.Load in its own function with a built in null check and console output.
     
    ThermalFusion likes this.