Quantcast
Channel: Questions in topic: "error"
Viewing all articles
Browse latest Browse all 7934

Error when adding a UI Text component to a gameobject?

$
0
0
Hi! I am making a stacking system so the player can stack items. But. When i add a UI Text component to show how many items are on that stack. I get an error. Here's my script: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class inventoryScript : MonoBehaviour { GameObject[] slotContentsGo = new GameObject[inventorySlotsCount]; public static int inventorySlotsCount = 52; public static Items[] slotContents = new Items[inventorySlotsCount]; public GameObject[] inventorySlotsAr = new GameObject[inventorySlotsCount]; void Start() { getSlots (); } static void hideInventory() { GameObject inventory = GameObject.Find ("Inventory"); for (int i = 0; i < inventory.transform.childCount; i++) { inventory.transform.GetChild (i).gameObject.SetActive (!inventory.transform.GetChild (i).gameObject.activeInHierarchy); } } public void takeItem(Items itemToTake) { for (int i = 0; i < slotContents.Length; i++) { if (itemToTake == slotContents [i] && itemToTake != null) { slotContents [i] = null; Destroy (slotContentsGo[i]); break; } } } public void clearInventory() { for (int i = 0; i < inventorySlotsCount; i++) { slotContents [i] = null; Destroy (slotContentsGo [i]); } } public void removeAtSlot(int slot) { for (int i = 0; i < slotContents.Length; i++) { if (i == slot) { if (slotContents [slot] != null) { slotContents [i] = null; Destroy (slotContentsGo [i]); } } } } public void addItem(Items itemToAdd) { for (int i = 0; i < slotContents.Length; i++) { if (slotContents [i] == null && itemToAdd != null && itemToAdd.stackable == false) { slotContents [i] = itemToAdd; GameObject returnedItem = createItem (itemToAdd.itemSprite, inventorySlotsAr [i].gameObject.transform, itemToAdd.itemName); slotContentsGo [i] = returnedItem; break; } } } // Relevant bit public GameObject createItem(Sprite imageSprite, Transform slot, string itemName) { // Here i am creating two gameobjects. The item. And the stack text // But i get an error when i try to add the Text component GameObject createObj = new GameObject (); GameObject stackText = new GameObject (itemName + " StackText"); createObj.AddComponent (); createObj.GetComponent ().sprite = imageSprite; createObj.transform.position = slot.position; createObj.transform.rotation = slot.rotation; createObj.transform.SetParent (slot.transform); createObj.transform.localScale = new Vector3 (slot.transform.localScale.x, slot.transform.localScale.y); createObj.name = itemName; stackText.transform.SetParent (createObj.transform); stackText.AddComponent (); stackText.transform.position = new Vector3 (slot.transform.position.x, slot.transform.position.y); stackText.transform.localScale = new Vector3 (slot.transform.localScale.x, slot.transform.localScale.y); stackText.AddComponent (); stackText.GetComponent ().fontSize = 45; stackText.GetComponent ().fontStyle = FontStyle.Bold; stackText.GetComponent ().alignment = TextAnchor.LowerLeft; stackText.GetComponent ().color = new Color (0, 0, 0, 255); return createObj; } void getSlots() { for (int i = 0; i < inventorySlotsCount; i++) { if (inventorySlotsAr [i] == null) { inventorySlotsAr [i] = GameObject.Find ("inventorySlot (" + i.ToString() + ")"); } if(i == 0) { inventorySlotsAr [i] = GameObject.Find ("inventorySlot"); } } } }

Viewing all articles
Browse latest Browse all 7934

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>