Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Iceblitzyt · Jul 28, 2013 at 09:07 PM · c#looting

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.

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 tw1st3d · Jul 28, 2013 at 10:59 PM 1
Share

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().

avatar image Iceblitzyt · Jul 29, 2013 at 11:42 AM 0
Share

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.

avatar image Chronos-L · Jul 29, 2013 at 12:09 PM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

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

18 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Loot Explosion 1 Answer

Renderer on object disabled after level reload 1 Answer

Initialising List array for use in a custom Editor 1 Answer


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