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 gongalone · Jul 31, 2013 at 06:12 PM · c#gameobjectinventory

Problem with taking a gameobject from gameobject list to inventory.

Hi, I have a little problem with my C# scripts i wrote inspired by some tutorials i recently read. In my little FPS scene, when player picks up an object of class MasterObject, my inventory class (in AddInventory function) checks if the item of that name is stored in prefabList class arrays. If it is it is returned by script in the ResourceManager to inventory. My problem is whenever I try to pick an object, Unity returns "NullReferenceException: Object reference not set to an instance of an object Resources.ResourceManager.GetWeapon (System.String name) (at Assets/Scripts/ResourceManager/ResourceManager.cs:18) Inventory.AddInventory () (at Assets/Scripts/Inventory/Inventory.cs:64) Inventory.Update () (at Assets/Scripts/Inventory/Inventory.cs:49) " I know my code may be a little bit messy, but I just started my adventure with C#.

Inventory Class

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using Resources;
 
 
 public class Inventory : MonoBehaviour {
     
     public FPSController controller;
     //private MasterObject mO;
     public bool isToggled = false;
     
     
     
     public GUISkin inventorySkin, inventoryEquip;
     
     public List<MasterObject> inventory = new List<MasterObject>();
     
     /****************************/
     
     private static int invID = 01;
     private Rect invWindow = new Rect(Screen.width/2,Screen.height/2, 500, 400);
     private int inventoryRows = 7;
     private int inventoryCols = 5;
         
     /****************************/
     
     private int buttonWidth = 40;
     private int buttonHeight = 40;
     
     private int invChangeWidth = 60;
     private int invChangeHeight = 20;
     
     /*****TEMP BUTTONS CONTROL****/
     
     public float buttonHorizontalPos;
     public float buttonVerticalPos;
     
     
     void Start () {
         
         
 
         controller = GetComponentInChildren<FPSController>();
     }
     
     void Update () {
             
     AddInventory();
     if(Input.GetKeyDown(KeyCode.I)) 
         {
             isToggled = !isToggled;
         }
 
 
     }
     
     void AddInventory()
     {
         
         MasterObject toPick = controller.pick;
         if(toPick != null)
         {
             GameObject item = Resources.ResourceManager.GetWeapon(toPick.name);
             //MasterObject itemToPick = item.GetComponentInChildren<MasterObject>();
             //inventory.Add(itemToPick);
                 
         }
                 
     }
     
     void OnGUI()
     {
 
         if(isToggled == true)
             {
                 drawInventory();
             }
 
     }
     
     public void drawInventory()
     {
         GUI.skin = inventorySkin;
         invWindow = GUI.Window(invID, invWindow, inventoryWindow, ".inventory()"); 
     }
     
     public void inventoryWindow(int id)
     {
         GUI.BeginGroup(new Rect(invWindow.width/2, 20, 240, 370), "");
         
         GUI.skin = inventoryEquip;
         GUI.Box(new Rect(0, 0, 240, 370), "");
         GUI.Button (new Rect(98.12f, 89.99f, buttonWidth, buttonHeight), "Chest");
         GUI.Button (new Rect(34.4f, 164.7f, buttonWidth, buttonHeight), "LH");
         GUI.Button (new Rect(160.4f, 22.81f, buttonWidth, buttonHeight), "Head");
         GUI.Button (new Rect(160.4f, 227.65f, buttonWidth, buttonHeight), "Leg");
         
         /**EQUIP CYBERWARE BUTTONS**/
         GUI.Button (new Rect(27.43f, 338.73f, invChangeWidth, invChangeHeight), "EQUIP");
         GUI.Button (new Rect(150.87f, 338.73f, invChangeWidth, invChangeHeight), "CYBER");
 
         GUI.EndGroup();
         GUI.BeginGroup(new Rect(10, 20, 240, 370), "");
         
         GUI.skin = null;
         GUI.Box(new Rect(0,0,240,370), "");
         for(int x = 0; x<inventoryCols; x++)
         {
             for(int y = 0; y < inventoryRows; y++)
             {
                 GUI.Button(new Rect(15 + (x * buttonWidth), 15 + (y * buttonHeight), buttonWidth, buttonHeight), "");
             }
         }
         GUI.EndGroup();
         GUI.DragWindow();
 
     }
             
 
 }
 

PrefabList class

 using UnityEngine;
 using System.Collections;
 using Resources;
 
 public class PrefabList : MonoBehaviour {
     
     public GameObject[] weapons;
     public GameObject[] consumables;
     public GameObject[] ammo;
     public GameObject[] cyberwares;
     public GameObject[] armor;
     public GameObject[] utility;
 
     void Start () {
     
     }
     
     void Update () {
     
     }
     
     public GameObject GetWeapon(string name)
     {
         for(int i = 0; i < weapons.Length; i++)
         {
             Weapon weapon = weapons[i].GetComponent<Weapon>();
             if(weapon && weapon.name == name) return weapons[i];
         }
         return null;
     }
 }
 

ResourceManager class

 using UnityEngine;
 using System.Collections;
 
 namespace Resources
 {
 public class ResourceManager : MonoBehaviour {
                 
         private static PrefabList prefablist;
         
         public static void SetPrefabList(PrefabList list)
         {
             prefablist = list;
         }
         
         public static GameObject GetWeapon(string name)
         {
             
             return prefablist.GetWeapon(name);
         }
         
         
 }
 }
 

MasterObject class (Weapon is MasterObject's subclass)

using UnityEngine; using System.Collections;

public class MasterObject : MonoBehaviour {

 public string objectName;
 public string objectClass;
 public Texture2D image;
 public int objectWeight;
 public string description;
 
 
 public GameObject go;
 public GameObject item;
 
 public bool isGrab = false;
 public bool isPick = false;
 
 public float acceleration = 0;
 
 private Vector3 myPosition;

 protected virtual void Start () {
     
     //go = GetComponent<GameObject>();
             
     objectName = this.name;
     objectClass = GetType().Name;

 }
 
 protected virtual void Update () {
     

     

 }

}

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 Wiki

Answer by davidoakley · Aug 01, 2013 at 02:44 PM

Hi gongalone,

I might be missing something here, but I can't see any calls to ResourceManager.SetPrefabList(), so the attempt to call prefablist.GetWeapon(name) in the ResourceManager.GetWeapon(name) method is getting a null reference on prefablist. Do you need to set that up in your MasterObject's Start() method?

Comment
Add comment · Show 1 · 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
avatar image gongalone · Aug 01, 2013 at 05:40 PM 0
Share

Hi $$anonymous$$,

Thanks for your reply. I'm not quite sure about calls to Resource$$anonymous$$anager.SetPrefabList(), I tried to ignore entire Resource$$anonymous$$anager part and take gameObject directly from PrefabList, but problem is still appearing. Error always sends me to the part of code that tries to acquire the returned object from GetWeapon function...

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

16 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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Creating multiple objects? 0 Answers

Drag and Drop icons into slots 1 Answer

Passing gameObjects to Another Script: NullReference Exception 1 Answer


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