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
-1
Question by HuskyPanda213 · Apr 22, 2013 at 03:58 AM · texturesstrings

Help with Texture2D's from a string name/path.

Hello im making a string based inventory(No its not list so don't tell me I should use one), but anyway Im getting errors in my code such as this, Cannot implicitly convert type string' to UnityEngine.Texture2D'. So can anyone tell me how to fix this error. Thanks.

This is my code:

 using UnityEngine;
 using System.Collections;
 
 public class Inventory : MonoBehaviour {
     [HideInInspector]
     public string Item1,Item2,Item3,Item4,Item5,Item6,Item7,Item8,Item9,Item10;
     [HideInInspector]
     public string Item11,Item12,Item13,Item14,Item15,Item16,Item17,Item18,Item19,Item20;
     [HideInInspector]
     public string Item21,Item22,Item23,Item24,Item25,Item26,Item27,Item28,Item29,Item30;
     [HideInInspector]
     public string Item31,Item32,Item33,Item34,Item35,Item36,Item37,Item38,Item39,Item40;
     [HideInInspector]
     public string Item41,Item42,Item43,Item44,Item45,Item46,Item47,Item48,Item49,Item50;
     
     [HideInInspector]
     public Texture2D CItem1,CItem2,CItem3,CItem4,CItem5,CItem6,CItem7,CItem8,CItem9,CItem10;
     [HideInInspector]
     public Texture2D CItem11,CItem12,CItem13,CItem14,CItem15,CItem16,CItem17,CItem18,CItem19,CItem20;
     [HideInInspector]
     public Texture2D CItem21,CItem22,CItem23,CItem24,CItem25,CItem26,CItem27,CItem28,CItem29,CItem30;
     [HideInInspector]
     public Texture2D CItem31,CItem32,CItem33,CItem34,CItem35,CItem36,CItem37,CItem38,CItem39,CItem40;
     [HideInInspector]
     public Texture2D CItem41,CItem42,CItem43,CItem44,CItem45,CItem46,CItem47,CItem48,CItem49,CItem50;
     
     public Rect Inventory_Rect = new Rect(20, 20, 200, 350);
     
     public Vector3 Scroll = Vector2.zero;
     
     public string IconPath = "Icons/Items/Weapons/";
     
     void Start () {
     
         Item1 = PlayerPrefs.GetString("Item1");
         Item2 = PlayerPrefs.GetString("Item2");
         Item3 = PlayerPrefs.GetString("Item3");    
         Item4 = PlayerPrefs.GetString("Item4");    
         Item5 = PlayerPrefs.GetString("Item5");    
         Item6 = PlayerPrefs.GetString("Item6");    
         Item7 = PlayerPrefs.GetString("Item7");    
         Item8 = PlayerPrefs.GetString("Item8");    
         Item9 = PlayerPrefs.GetString("Item9");    
         Item10 = PlayerPrefs.GetString("Item10");
         Item11 = PlayerPrefs.GetString("Item11");
         Item12 = PlayerPrefs.GetString("Item12");
         Item13 = PlayerPrefs.GetString("Item13");    
         Item14 = PlayerPrefs.GetString("Item14");    
         Item15 = PlayerPrefs.GetString("Item15");    
         Item16 = PlayerPrefs.GetString("Item16");    
         Item17 = PlayerPrefs.GetString("Item17");    
         Item18 = PlayerPrefs.GetString("Item18");    
         Item19 = PlayerPrefs.GetString("Item19");    
         Item20 = PlayerPrefs.GetString("Item20");
         Item21 = PlayerPrefs.GetString("Item21");
         Item22 = PlayerPrefs.GetString("Item22");
         Item23 = PlayerPrefs.GetString("Item23");    
         Item24 = PlayerPrefs.GetString("Item24");    
         Item25 = PlayerPrefs.GetString("Item25");    
         Item26 = PlayerPrefs.GetString("Item26");    
         Item27 = PlayerPrefs.GetString("Item27");    
         Item28 = PlayerPrefs.GetString("Item28");    
         Item29 = PlayerPrefs.GetString("Item29");
         Item30 = PlayerPrefs.GetString("Item30");    
         Item31 = PlayerPrefs.GetString("Item31");
         Item32 = PlayerPrefs.GetString("Item32");
         Item33 = PlayerPrefs.GetString("Item33");    
         Item34 = PlayerPrefs.GetString("Item34");    
         Item35 = PlayerPrefs.GetString("Item35");    
         Item36 = PlayerPrefs.GetString("Item36");    
         Item37 = PlayerPrefs.GetString("Item37");    
         Item38 = PlayerPrefs.GetString("Item38");    
         Item39 = PlayerPrefs.GetString("Item39");    
         Item40 = PlayerPrefs.GetString("Item40");
         Item41 = PlayerPrefs.GetString("Item41");
         Item42 = PlayerPrefs.GetString("Item42");
         Item43 = PlayerPrefs.GetString("Item43");    
         Item44 = PlayerPrefs.GetString("Item44");    
         Item45 = PlayerPrefs.GetString("Item45");    
         Item46 = PlayerPrefs.GetString("Item46");    
         Item47 = PlayerPrefs.GetString("Item47");    
         Item48 = PlayerPrefs.GetString("Item48");    
         Item49 = PlayerPrefs.GetString("Item49");    
         Item50 = PlayerPrefs.GetString("Item50");    
         
         
         
     }
     
     void Update () {
         CItem4 = Resources.Load(IconPath) as Texture2D;
         
         CItem1 = IconPath + Item1;
         
     }
     
     void OnGUI(){
         Inventory_Rect = GUI.Window(0, Inventory_Rect, InventoryHUD, "Inventory");
     }
     
     private void InventoryHUD(int windowID){
         
           Scroll = GUILayout.BeginScrollView(Scroll);
         
         GUILayout.BeginHorizontal();
         
         if(GUI.Button(new Rect(5,10,40,40),CItem1)){}
         if(GUI.Button(new Rect(50,10,40,40),CItem2)){}
         if(GUI.Button(new Rect(95,10,40,40),CItem3)){}
         if(GUI.Button(new Rect(140,10,40,40),CItem4)){}
         
         GUILayout.EndHorizontal();
         
         GUILayout.EndScrollView();
         
         GUI.DragWindow();
     }
     
     public void AddItem(){
         
     }
 }
Comment
Add comment · Show 1
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 Loius · Apr 22, 2013 at 04:26 AM 0
Share

i am sooo jealous. you're gonna have soo much fun doing that without arrays of any kind! invite me to the party~~

strings aren't textures, you'll need to load it somehow. it's a good idea to indicate the problem line when asking for help; no one likes to dig through foreign code

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · Apr 22, 2013 at 04:25 AM

Your issue you describe is on line 93. CItem1 is a Texture2D. "IconPath + Item1" is a string. You cannot assign a string to a texture. I'm not sure what you are trying to do here. Perhaps what you want is:

 CItem1 = Resources.Load(IconPath + Item1) as Texture2D;

There is also an issue on line 91. You are using Resources.Load() but you are only passing a path (no file identifier), so the load will always fail.

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

12 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

Related Questions

Multiple Cars not working 1 Answer

packing textures for ready use 1 Answer

I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer

Pickuptype script keeps ringing Compiler errors 2 Answers

Accessing variables from different scripts. 3 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