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 HuskyPanda213 · Aug 10, 2013 at 05:38 AM · guitexture2dinventory

GUI error with texture.

My GUI button is having an error I have an code assigned texture and so on, but the only thing the button does is say Firewood(Unity Engine Texture) in stead of showing a texture2d,(Firewood is the texture file name). I don't know why, please help(My texture is 40x40, and so is the button). Cheers.

Heres the script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class PlayerInventory : MonoBehaviour {
     
     public List<Item> items = new List<Item>();
     
     public string PrimaryS;
     public string SecondaryS;
     
     public Gun Primary;
     public Gun Secondary;
     [HideInInspector]
     public Texture2D gun1icon;
     [HideInInspector]
     public Texture2D gun2icon;
     
     public GUIStyle gunStyle;
     
     public GUIStyle itemStyle;
     
     private int rows = 4;
     private int cols = 4;
     
     private Rect windowRect = new Rect(150,150,200,300);
     
     private bool isWindow;
     
     public static PlayerInventory ins;
     
     // Use this for initialization
     void Start () {
     ins = this;
     }
     
     // Update is called once per frame
     void Update () {
 //    gun1icon = Resources.Load("Icons/" + PrimaryS);
 //    gun2icon = Resources.Load("Icons/" + PrimaryS);
         
     if(Input.GetKeyUp(KeyCode.G)){
         isWindow = !isWindow;
     }
     }
     
     void LateUpdate(){
         foreach(Item im in items){
         im.texture = Resources.Load("Icons/" + im._name) as Texture2D;
         im.BuiltPrefab = Resources.Load("Prefabs/" + im._name) as GameObject;
         }
     }
     
     void OnGUI(){
         //Start Item List
         if(isWindow)
         windowRect = GUI.Window(0,windowRect,WindowPlayer,"Gear");
         
     }
     
     public void AddAnItem(string nameofItem){
         Item temp = new Item();
         temp._name = nameofItem;
         temp.texture = Resources.Load("Icons/" + nameofItem) as Texture2D;
         temp.BuiltPrefab = Resources.Load("Prefabs/" + nameofItem) as GameObject;
         items.Add(temp);
     }
     
     public void RemoveItemFromList(int _PositionInList){
         items.RemoveAt(_PositionInList);
     }
     
     public void WindowPlayer(int windowID){
         if(GUI.Button(new Rect(25,20,150,50),"")){}
         if(GUI.Button(new Rect(25,75,150,50),"")){}
         
         int cnt = 0;
         for(int y = 0; y < rows; y++){
             for(int x = 0; x < cols; x++){
                 if(cnt < items.Count){
                     if(GUI.Button(new Rect(20 + x * 40, 130 + y * 40,40,40),"" + items[cnt].texture)){}
                 }
                 else{
                     GUI.Box(new Rect(20 + x * 40, 130 + y * 40,40,40),"");
                 }
                 cnt++;
             }
         }
         GUI.DragWindow();
     }
 }
 
 [System.Serializable]
 public class Item{
     public string _name;
     public Texture2D texture;
     public GameObject BuiltPrefab;
     public bool isCounted;
     public bool isCountedAsNull;
 }
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
Best Answer

Answer by Joyrider · Aug 10, 2013 at 11:34 AM

Hehe, that sounds like it is converted to a string, which I sometimes use in debugs.

I'm guessing the problem is at line 81:

  if(GUI.Button(new Rect(20 + x * 40, 130 + y * 40,40,40),"" + items[cnt].texture)){}

remove the "" + , it indicates the type is a string, and thus your texture is interpreted as one, giving you: ObjectName(ObjectType).

So it should be :

  if(GUI.Button(new Rect(20 + x * 40, 130 + y * 40,40,40),items[cnt].texture)){}
Comment
Add comment · Show 1 · 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 HuskyPanda213 · Aug 10, 2013 at 03:19 PM 0
Share

Thank you so much. I totally missed that. :)

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

15 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

Related Questions

Populate inventory with item icons 1 Answer

Inventory, why didn't work element? 1 Answer

My OnGUI() Won't show the Button elements :( 0 Answers

Draw crosshair INSTEAD of cursor. 1 Answer

Click on a button that is created post start. 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