Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 ShaggySlade · Dec 23, 2016 at 03:38 AM · uiinventorydisplaywindowlooting

chests and looting with ui

I hope Im asking this right. Ill try to explain as best i can. EDIT....maybe these pics will help explain alt text alt text

Even thugh i cant upload the 3rd pic of me opening the 1st chest again....I assure you all it shows different from the 2nd pic is the debug list shows the item names from the 1st chest.

Ive been following BergZergArcade's tutorials on youtube.(here is where im at : https://www.youtube.com/watch?v=_tLMM5EfB3M&list=PLE5C2870574BF4B06∈dex=122). Anyway its an old but well made tutorial but I would like to use Unity's UI system instead of OnGUI. So far Ive managed to figure it out with some tinkering but I have hit a wall and cannot figure this out.

The problem:

I have a loot window(scroll view with a panel) to display the items in a chest or corpse. it displays the items just fine the way I want them to BUT if I open a 2nd or 3rd chest it just adds to the list of items from the first chest instead of displaying just the contents of the newly opened chest. I guess you could say its acting more like an inventory the a loot window.

Anyone know anything about how to get the loot display window to just display the current chest ?

EDIT: Here is the two scripts related to what Im working on.

First the Chest script(attached to my Chest GameObject):

   using UnityEngine;
   using System.Collections;
   using System.Collections.Generic;
   
   
   
   [RequireComponent (typeof(BoxCollider))]
   [RequireComponent(typeof(AudioSource))]
   public class Chest : MonoBehaviour {
   
   
       public enum State
       {
           open,
           close,
           inbetween
       }
   
       public AudioClip openSound;
       public AudioClip closeSound;
   
       public State state;
   
       public float maxDistance = 2.4f;           //Max distance the player can be to open this chest
   
       private GameObject _player;
       private Transform _myTransform;
       public bool inUse = false;
   
       private GameObject lootWindow;
       private GameObject LWitemHolder;
       public GameObject item;
   
       private bool _used = false;                 //track if the chest has been used or not
   
       public List<Item> loot = new List<Item>();
   
       // Use this for initialization
       void Start () {
           lootWindow = GameObject.Find("Loot Window");
           LWitemHolder = GameObject.Find("LW item Holder");
           _myTransform = transform;
           state = State.close;
   
           
   
       }
   
       // Update is called once per frame
       void Update () {
           if (!inUse)
           {
               return;
           }
   
           if (_player == null)
           {
               return;
           }
   
           if(Vector3.Distance(_myTransform.position, _player.transform.position) > maxDistance)
           {
               playerHUD.chest.ForceClose();
           }
           return;
       }
   
       public void OnMouseEnter()
       {
       }
   
   
       public void OnMouseExit()
       {
       }
   
   
       public void OnMouseUp()
       {
   
           GameObject go = GameObject.FindGameObjectWithTag("Player");
   
           if(go == null)
           {
               return;
           }
           if(Vector3.Distance(_myTransform.position, go.transform.position) > maxDistance && !inUse)
           {
               return;
           }
   
   
           switch (state)
           {
               case State.open:
                   state = State.inbetween;
                   ForceClose();
                   break;
               case State.close:
                   if(playerHUD.chest != null)
                   {
                       playerHUD.chest.ForceClose();
                   }
                   state = State.inbetween;
                   StartCoroutine("Open");
                   break;
           }
       }
   
       private IEnumerator Open()
       {
     //set this script to be the one that is holding the items
           playerHUD.chest = this;
   
     //find the player so its distance can be trcked after opening the chest
           _player = GameObject.FindGameObjectWithTag("Player");
   
           inUse = true;
   
           GetComponent<Animation>().Play("open");
           GetComponent<AudioSource>().PlayOneShot(openSound);
   
           if (!_used)
           {
           PopulateChest(5);
           }
   
      //wait until the chest is done opening
           yield return new WaitForSeconds(GetComponent<Animation>()["open"].length - .5f);
           state = State.open;
   
           Messenger.Broadcast("DisplayLoot");
       }
   
       private void PopulateChest(int x)
       {
           
   
           for (int cnt = 0; cnt < x; cnt++)
           {
                       
               loot.Add(new Item());
               loot[cnt].Name = "I:" + Random.Range(0, 100);
               item = Instantiate(item);
               item.transform.SetParent(LWitemHolder.transform, false);
               item.name = loot[cnt].Name;
           }
           _used = true;
       }
   
       private IEnumerator Close()
       {
           _player = null;
           inUse = false;
   
           GetComponent<Animation>().Play("close");
           yield return new WaitForSeconds(GetComponent<Animation>()["close"].length);
           GetComponent<AudioSource>().PlayOneShot(closeSound);
           state = State.close;
   
   
       }
   
       public void ForceClose()
       {
           Messenger.Broadcast("CloseChest");
   
           StopCoroutine("Open");
           StartCoroutine("Close");
   
       }
   
   }

Next is my playerHUD script(attached to an empty GameObject):

   using UnityEngine;
   using System.Collections;
   using System.Collections.Generic;
   
   
   public class playerHUD : MonoBehaviour {
   
       public GameObject lootWindow;
       public GameObject LWitemHolder;
   
       public static Chest chest;
   
       // Use this for initialization
       void Start () {
           lootWindow.SetActive(false);
       }
       
       private void OnEnable()
       {
           Messenger.AddListener("DisplayLoot", DisplayLoot);
           Messenger.AddListener("CloseChest", LootWindowClose);
       }
   
       private void OnDisable()
       {
           Messenger.RemoveListener("DisplayLoot", DisplayLoot);
           Messenger.RemoveListener("CloseChest", LootWindowClose);
   
       }
   
       // Update is called once per frame
       void Update () {
       
       }
   
       private void DisplayLoot()
       {
           Debug.Log(chest.loot.Count);
   
           
   
           lootWindow.SetActive(true);
       }
   
       public void LootWindowClose()
       {
           
           chest.OnMouseUp();
           
           chest = null;
           lootWindow.SetActive(false);
           
       }
   }


1st-chest.jpg (280.9 kB)
2nd-chest.jpg (374.4 kB)
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

Answer by TobiasjBurford · Apr 13 at 01:23 AM

Sorry i'm a bit late, but you could try changing the list to private? that would make it so that only one chest has access to it, (the specific chest) unless you need it.

Comment
Add comment · 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

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

115 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

Related Questions

How do you make a UI loot window display only the current chest/corpse that is open? 0 Answers

Null Reference Exception for Custom Editor Window 0 Answers

Display list as UI or GUI 0 Answers

Unity UI Problems,Elements Ui have some problems 0 Answers

Having constant trouble with Unity's inbuilt UI system. 2 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