nullreferenceexception code example
Example 1: exception is null c#
//Bug with VS: Exception e == null
//Happens when multiple catch variables are the same
//an example of the solution:
try
{
// do something
}
catch (WebException webEx) // using a variable named 'webEx' for this catch
{
Logger.Log("Error while tried to do something. Error: " + webEx.Message); // <-
}
catch (Exception ex) // using a DIFFERENT variable for this one
{
Logger.Log("Error while tried to do something. Error: " + ex.Message);
}
Example 2: nullreferenceexception unity
// to change a variable from another script, just use this code with 1 being the
//GameObject that the script is on, 2 being the script, and 3 being the variable
GameObject.Find("1").GetComponent<2>().3 = whatever you want to change it to; ;