Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by malkere · Oct 22, 2014 at 03:40 AM · guistaticnull reference exceptionwrap

Can't call a class variable from a class from the GUI

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by malkere · Oct 22, 2014 at 03:45 AM

I seem to do this whenever I ask a question =/ after fighting with it for two days I answered it myself as soon as I posted it...

I moved the subrecipe to its own script and now they are interacting fine....

I still don't understand why I can't program it the way I did above? Can anyone answer that?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Displaying a static variable from another script with OnGUI 1 Answer

Automatic Wrap 2 Answers

car controls gui 0 Answers

Developing a Dynamic GUI 1 Answer

NullReferenceException: Object reference not set to an instance of an object 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges