Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Vanishedcandy27 · May 04, 2019 at 05:01 PM · editorarraysclassconstructor

Code only runs when object is selected in the editor

I have been working on a "Diablo Style" inventory system for my game. The whole script is run off of the Canvas object. If I run the game with the Canvas selected in the editor, everything runs smoothly. However, if I select any other object, or build the game, the Inventory doesn't build correctly.

I think that it has something to do with a private array that wont let me initialize it. If I make it public and set the values in the inspector window, It works fine. However, if I make it private and initialize it through the code, the inventory doesn't build.

This is a simplified version of the code:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

[System.Serializable] public class SlotData { public GameObject slot; public bool isFull;

 public SlotData()
 {
     slot = null;
     isFull = false;
 }

}

[System.Serializable] public class ItemData { public GameObject item; public GameObject icon; public Vector2Int size; public GameObject slot; public GameObject pickup;

 public ItemData()
 {
     item = null;
     icon = null;
     size = Vector2Int.zero;
     slot = null;
     pickup = null;
 }

}

public class InventoryManager : MonoBehaviour { public Vector2Int inventorySize; public Sprite inventoryTexture; public Sprite cursorImage;

 private GameObject inventoryBackground;
 private GameObject inventorySlots;
 private GameObject inventoryItems;
 private GameObject cursor;

 [HideInInspector]
 public GameObject heldItem;
 private Vector2Int heldItemDimensions;
 private int heldItemNumber;
 private GameObject heldItemIcon;

 public ItemData[] items;
 public SlotData[] slots;
 private int numberOfItems;

 private bool toggleBool;
 private bool InventoryIsOpen;

 private GameObject maxSlot;
 private GameObject minSlot;

 void Start () 
 {
     items = new ItemData[inventorySize.x * inventorySize.y];
     slots = new SlotData[inventorySize.x * inventorySize.y];

     CreateInventory(inventorySize, inventoryTexture, inventoryBackground);
     CreateSlots(inventorySize, inventoryTexture);
 }

void CreateInventory (Vector2 size, Sprite texture, GameObject inventory) { inventory = new GameObject("Inventory"); RectTransform back = inventory.AddComponent(); inventory.transform.SetParent(this.transform); back.anchorMin = new Vector2((1 - (size.x / 10)) / 2, (1 - (size.y / 10)) / 2); back.anchorMax = new Vector2(1 - ((1 - (size.x / 10)) / 2), 1 - ((1 - (size.y / 10)) / 2)); inventory.AddComponent().sprite = texture; inventory.GetComponent().type = Image.Type.Sliced; back.anchoredPosition = Vector2.zero; back.sizeDelta = Vector2.zero; inventoryBackground = inventory;

     // Slot Holder
     inventorySlots = new GameObject("Inventory Slots");
     inventorySlots.transform.SetParent(inventory.transform);
     inventorySlots.AddComponent<RectTransform>().anchorMin = Vector2.zero;
     inventorySlots.GetComponent<RectTransform>().anchorMax = Vector2.one;
     inventorySlots.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
     inventorySlots.GetComponent<RectTransform>().sizeDelta = Vector2.zero;

     // Icon Holder
     inventoryItems = new GameObject("Inventory Icons");
     inventoryItems.transform.SetParent(inventory.transform);
     inventoryItems.AddComponent<RectTransform>().anchorMin = Vector2.zero;
     inventoryItems.GetComponent<RectTransform>().anchorMax = Vector2.one;
     inventoryItems.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
     inventoryItems.GetComponent<RectTransform>().sizeDelta = Vector2.zero;

     // Cursor
     cursor = new GameObject("Cursor");
     cursor.transform.SetParent(inventory.transform);
     cursor.gameObject.AddComponent<Image>().sprite = cursorImage;
     cursor.GetComponent<RectTransform>().localScale = new Vector2(0.25f, 0.25f);
     cursor.GetComponent<RectTransform>().pivot = new Vector2(0, 1);
 }

 void CreateSlots (Vector2 size, Sprite texture)
 {
     int i = 0; 
     for(int x = 0; x < size.x; x++)
     {
         for(int y = 0; y < size.y; y++)
         {
             i++;
             GameObject slot = new GameObject("Slot " + (x + 1) + ", " + (y + 1));
             slot.transform.SetParent(inventorySlots.transform);
             slot.AddComponent<Image>().sprite = texture;
             slot.GetComponent<Image>().type = Image.Type.Sliced;
             slot.GetComponent<RectTransform>().anchorMin = new Vector2(0.01f + ((0.98f / size.y) * y), 0.01f + ((0.98f / size.x) * x));
             slot.GetComponent<RectTransform>().anchorMax = new Vector2(0.01f + ((0.98f / size.y) * (y + 1)), 0.01f + ((0.98f / size.x) * (x + 1)));

             slot.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
             slot.GetComponent<RectTransform>().sizeDelta = Vector2.zero;
             slots[i - 1].slot = slot.gameObject;
         }
     }
 }

}

It always seems to stop working at CreateSlots with a NullReferenceException. Did I initialize or construct the arrays wrong?

Comment
Add comment · Show 1
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 Vanishedcandy27 · May 04, 2019 at 05:03 PM 0
Share

The code really got messed up when I pasted it in. Sorry about that, I'm not sure how to fix it.

0 Replies

· Add your reply
  • Sort: 

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

205 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 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 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 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 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 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 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 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

Weird Script behaviour 1 Answer

How to create a drop down menu in editor inspector 0 Answers

Cannot Set Array Size in Inspector 2 Answers

How to save a two-dimensional array, as part of the variable inspector? 0 Answers

Crear un arreglo de una clase en otra clase 0 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