- Home /
Toggle Inventory script
I created an inventory system with Canvas where a script instantiate a number of slots in it. Now i want to create a script that enable/disable the inventory and can block the movement of the player. But there is a problem: if I start with the inventory disabled, when you toggle it will be without slots... and the odd thing is that slots are created, but they don't show into the Canvas.
The script is this:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ToggleInventory : MonoBehaviour {
public bool toggleInventory = true;
private PlayerMovement move;
private CameraMouseLook look;
private Canvas canv;
void Start ()
{
GameObject player = GameObject.FindGameObjectWithTag ("Player");
GameObject cam = GameObject.FindGameObjectWithTag ("MainCamera");
move = player.GetComponent<PlayerMovement> ();
look = cam.GetComponent<CameraMouseLook> ();
canv = GetComponent<Canvas> ();
}
void Update ()
{
if (Input.GetButtonDown ("Inventory") && toggleInventory == true)
{
canv.enabled = !canv.enabled;
move.playerBlocked = !move.playerBlocked;
look.blockCamera = !look.blockCamera;
}
}
}
Anyone help?
I dont see a mistake in this script the wrong part has to be on some other script or some editor preferences the only thing is that u dont need this: "toggleInventory" because its always true but that isnt causing the mistake
This is the script that instantiate the inventory slots
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class Inventory_System : $$anonymous$$onoBehaviour {
public int maxSlots;
public GameObject inventorySlot;
public GameObject inventoryIcon;
public List<Item> itemObj = new List<Item> ();
public List<GameObject> slots = new List<GameObject> ();
private GameObject inventoryPanel;
private GameObject slotPanel;
private Item_Database database;
void Start ()
{
database = GetComponent<Item_Database> ();
inventoryPanel = GameObject.Find ("InvPanel");
slotPanel = inventoryPanel.transform.FindChild ("InvSlot").gameObject;
for (int i = 0; i < maxSlots; i++)
{
itemObj.Add (new Item());
slots.Add (Instantiate (inventorySlot));
slots [i].transform.SetParent (slotPanel.transform);
}
AddItem (1);
}
public void AddItem(int id)
{
Item itemToAdd = database.FetchItemByID (id);
for (int i = 0; i < itemObj.Count; i++)
{
if (itemObj [i].ID == -1)
{
itemObj [i] = itemToAdd;
GameObject objects = Instantiate (inventoryIcon);
objects.transform.SetParent (slots[i].transform);
objects.transform.localPosition = Vector2.zero;
objects.GetComponent<Image> ().sprite = itemToAdd.Sprite;
break;
}
}
}
}
Answer by bromley · May 08, 2016 at 03:30 PM
ok, i understand the problem: if i start with Canvas disabled, the inventory_system script can't call the panel and the slots of the Canvas, so the invenorty results without them
the new script is this:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ToggleInventory : MonoBehaviour {
public bool toggleInventory = true;
private PlayerMovement move;
private CameraMouseLook look;
private Canvas canv;
void Start ()
{
canv = GetComponent<Canvas> ();
if (canv.enabled == true)
{
canv.enabled = false;
}
GameObject player = GameObject.FindGameObjectWithTag ("Player");
GameObject cam = GameObject.FindGameObjectWithTag ("MainCamera");
move = player.GetComponent<PlayerMovement> ();
look = cam.GetComponent<CameraMouseLook> ();
}
void Update ()
{
if (Input.GetButtonDown ("Inventory") && toggleInventory == true)
{
canv.enabled = !canv.enabled;
move.playerBlocked = !move.playerBlocked;
look.blockCamera = !look.blockCamera;
}
}
}
in this case the Canvas starts enabled, but on void Start() the Canvas will disable immediately, but the inventory_system script can call the panel and the slots
Nice :) Like i sad no problem with that script you posted :)