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