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 /
avatar image
0
Question by bromley · May 07, 2016 at 07:47 AM · canvasinventorytoggleinventory systemslot

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?

Comment
Add comment · Show 3
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 bromley · May 07, 2016 at 05:11 PM 0
Share

does somebody help me?

avatar image TheSorm · May 07, 2016 at 09:21 PM 1
Share

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

avatar image bromley · May 08, 2016 at 11:01 AM 0
Share

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;
             }
         }
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 2 · 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 TheSorm · May 08, 2016 at 04:38 PM 1
Share

Nice :) Like i sad no problem with that script you posted :)

avatar image AR0106 · Aug 06, 2016 at 04:34 PM 0
Share

It says there are 17 errors please fix them

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

44 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

Related Questions

Best approach in making Inventory UseItem functionality. 1 Answer

How do i reference if toggle is on or off? 0 Answers

How do I make my item only open if there is room in my Inventory? 3 Answers

Adventure Game tutorial, Replacing models in the game world? 0 Answers

how to detect wich item i have in my inevntory 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