- Home /
can someone help me with my looting script? c#
Hello there,
So I made this script which I'll show below which basically presents a looting screen. It's fairly basic but it works under the principle when the function populate is called the number of items denoted as x will appear in the box. This is the script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class LootWindow : MonoBehaviour {
public GUIStyle EButton; // 20 by 20
public GUIStyle LootWindowGUI;
//LootWindow orientated
public const int LootWindowID = 1;
public Rect LootWindowRect = new Rect(0,0,0,0);
public List<ItemDataBase> LootItems;
public Vector2 WindowSlider = Vector2.zero;
public bool OpenLootWindow = false;
void Start () {
LootItems = new List<ItemDataBase>();
}
void OnGUI(){
if(OpenLootWindow){
LootWindowRect = GUI.Window(LootWindowID,new Rect(Screen.width/2-100,Screen.height/2-200,220,300),LootWindowOpen,"Loot Window",LootWindowGUI);
}
}
public void LootWindowOpen(int id){
WindowSlider = GUI.BeginScrollView( new Rect(5,55,LootWindowRect.width - 10, 300),WindowSlider,new Rect(5,18,60*2,(60 * 7) + 65));
for(int i=0; i < 3; i++){
for( int y =0; y < 7; y++){
GUI.Button(new Rect(10 + (i * 60),20 + (y * 60),60,60),"T" + (i + y * 5));
}
}
GUI.EndScrollView();
}
public void Populate(int x){
for(int i=0; i < x; i++){
LootItems.Add( new ItemDataBase());
OpenLootWindow = true;
}
}
public void OnEnable(){
EnemyDeath ed = GetComponent<EnemyDeath>();
ed.displayloot = true;
}
public void OnDisable(){
EnemyDeath ed = GetComponent<EnemyDeath>();
ed.displayloot = false;
}
void Update () {
}
}
Here is the script that calls it, the basic enemy death:
using UnityEngine;
using System.Collections;
public class EnemyDeath : MonoBehaviour {
public bool displayloot = false;
void Update () {
if(Input.GetKeyDown(KeyCode.Y) && displayloot ==false){
displayloot = true;
GetComponent<LootWindow>().enabled = true;
LootWindow lw = GetComponent<LootWindow>();
lw.Populate(5);
}
else if(Input.GetKeyDown(KeyCode.Y) && displayloot ==true){
displayloot = false;
GetComponent<LootWindow>().enabled = false;
}
}
}
As you can see in the enemy death i wanted to call 5 items in populate, but this doesn't happen at all.
Can someone render their assistance please?
Many thanks.
I don't know where you're getting these classes and list enum types, but one thing that I do see is that you're trying to create GUI elements outside of OnGUI(), which you can't do. Try calling your custom methods inside of OnGUI().
The scripting works, when I'm calling items from the item list. It's done in functions because thats what is required when you use a GUI window. What happening is the GUI window is on the ONGUI but is being called in the functions below by the code. If i wanted to make it so that the looting system calls the items from the Item class then it would work, what im trying to do is make only 5 display in the window by sending the number 5 to the function which should in theory when called by the OnGUI make 5 display. This however doesn't work.
As you can see in the enemy death i wanted to call 5 items in populate, but this doesn't happen at all.
This doesn't happen at all, how sure are you? You didn't see anything and you just came to the conclusion that it did not happen?
Have you use a simple Debug.Log
to check whether your Populate()
is called and to check the size of your LootItems
?
And do you know that your LootItems
is used in the Start()
and the Populate()
in your code, not in any of the GUI related code?
Answer by abeldantas · Jul 29, 2013 at 12:59 PM
If in LootWindow you put a debug
void OnGUI(){
if(OpenLootWindow){
LootWindowRect = GUI.Window(LootWindowID,new Rect(Screen.width/2-100,Screen.height/2-200,220,300),LootWindowOpen,"Loot Window",LootWindowGUI);
}
if (LootItems != null)
{
Debug.Log("We have " + LootItems.Count + " items");
}
}
You will see that once you hit the "Y", LootItems List gets populated with 5 things. The problem is that your not doing anything with the LootItems List inside LootWindowOpen.
Other than that, you should have your Rects public and initialize them in the Start function, like you did for LootWindowRect.
While debugging I changed your code a bit, take a look: http://pastebin.com/7aWXkAXF
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Loot Explosion 1 Answer