I have class type Recipe {_name, _description}
and a class type SmithingRecipe {_name, _description, _recipe1, _recipe2}
within each SmithingRecipe there are Recipes required to complete it. But when I try to access them from my GUI I get Null Reference for going through the SmithingRecipe to get its _recipe1._name:
Recipe script:
using UnityEngine;
using System.Collections;
public class Recipe {
public string _name { get; set; }
public string _description { get; set; }
public string _mat1 { get; set; }
public int _mat1Amount { get; set; }
public Recipe (string name, string description, string material1, int material1Amount) {
_name = name;
_description = description;
_mat1 = material1;
_mat1Amount = material1Amount;
}
}
SmithingRecipe script:
using UnityEngine;
using System.Collections;
public class SmithingRecipe {
public string _name { get; set; }
public string _description { get; set; }
public Recipe _recipe1 { get; set; }
//public Recipe _recipe2 { get; set; }
//public Recipe _recipe3 { get; set; }
public SmithingRecipe (string name, string description, Recipe recipe1) {
_name = name;
_description = description;
_recipe1 = recipe1;
//_recipe2 = null;
//_recipe3 = null;
}
}
SmithingRecipes hard code script:
using UnityEngine;
using System.Collections;
public static class SmithingRecipes {
//----------------------
//-----------Recipes----
//----------------------
// SmithingRecipe (string name, string description, Recipe recipe1)
public static SmithingRecipe BasicChestPlate = new SmithingRecipe("Basic Chest Plate",
"The simplest of chest plate armors.",
SmithingRecipes.MetalPlate);
//----------------------
//-----------Sub-Recipes
//----------------------
// Recipe (string name, string description, string material1, int material1Amount)
public static Recipe MetalPlate = new Recipe("Metal Plate",
"A flat metal plate used in various applications",
"MetalLump", 2);
}
GUI script condensed:
using UnityEngine;
using System.Collections;
public class SmithingRecipesGUI : MonoBehaviour {
void OnGUI () {
if (windowRecipesOpen){ GUI.Window(4, new Rect(Screen.width /10, Screen.height / 10, _80w, _80h), showRecipes, "Blacksmithing Recipes", basicWindow); }
}
public void showRecipes(int windowID){
//window is 80% x 80%
GUI.Box (new Rect(5,5,_80w / 3, _80h - 20), "Recipes:");
if (GUI.Button(new Rect(10,40,(_80w / 3) - 20,30), "Basic Chest Plate")) {
//_currentRecipe = SmithingRecipes.BasicChestPlate;
}
GUI.Box (new Rect(_80w / 3 + 10,5,(_80w / 3) * 2 - 20, _80h - 20), "");
GUI.Label (new Rect(_80w / 3 + 20,15,(_80w / 3) * 2 - 20, 20), SmithingRecipes.MetalPlate._name);
GUI.Label (new Rect(_80w / 3 + 20,40,(_80w / 3) * 2 - 20, 40), SmithingRecipes.MetalPlate._description);
GUI.Label (new Rect(_80w / 3 + 20,80,100,20), SmithingRecipes.BasicChestPlate._recipe1._mat1);
GUI.Label (new Rect(_80w / 3 + 120,80,30,20), SmithingRecipes.BasicChestPlate._recipe1._mat1Amount.ToString());
if (GUI.Button(new Rect(_80w - 100,100,80,40), "Craft!")) {
//start
//_currentRecipe = SmithingRecipes.BasicChestPlate;
}
}
//GUI.DragWindow(); //not working
}
}
the line where the BasicChestPlate._name is called comes out fine, but when I try to go through that and call BasicChestPlate._recipe1._name I get a null reference exception: NullReferenceException: Object reference not set to an instance of an object
SmithingRecipesGUI.showRecipes (Int32 windowID) (at Assets/Scripts/SmithingRecipesGUI.cs:48)
UnityEngine.GUI.CallWindowDelegate (UnityEngine.WindowFunction func, Int32 id, UnityEngine.GUISkin _skin, Int32 forceRect, Single width, Single height, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/GUI.cs:1408)
I’m confused why when the MetalPlate recipe is statically hard coded it cannot be accessed in this manner? I was hoping to be able to set up automatic functions this way but just calling AvailableSmithingRecipe1._recipe1._name etc for when the recipe book gets full of different kinds of recipes.
Thanks for any insight.