• 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 /
This question was closed May 31, 2014 at 02:43 PM by thornekey for the following reason:

The question is answered, right answer was accepted

avatar image
Question by thornekey · May 31, 2014 at 01:51 PM · texturevariableplayerprefs

string plus int equals the name of the variable? How?

I have 6 clothing textures.

 public Texture2D clothe1;
 public Texture2D clothe2;
 public Texture2D clothe3;
 public Texture2D clothe4;
 public Texture2D clothe5;
 public Texture2D clothe6;

I wish to assign one of them to a material. It will be done by checking a player prefs int variable.

The variable is assigned to an int variable called "Top"

So I basically want to say something like

         gameObject.renderer.material.mainTexture = "clothe" + Top.ToString();

emphasis on ""clothe" + Top.ToString();"

because then it equals clotheWHATEVERTHENUMBERIS

which would equal one of the textures..

But its not working. How would I achieve this?

Comment

People who like this

0 Show 0
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

3 Replies

  • Sort: 
avatar image
Best Answer

Answer by thornekey · May 31, 2014 at 02:43 PM

nevermind, i solved it like this:

 if(Top == 1) {
             gameObject.renderer.material.mainTexture = clothe1;
         } else 
         if(Top == 2) {
             gameObject.renderer.material.mainTexture = clothe2;
Comment

People who like this

0 Show 3 · 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 meat5000 ♦ · May 31, 2014 at 02:47 PM 1
Share

I knew you would do it this way, so I suggested a Switch Statement :D

 switch (Top)
 {
     case 1:
         gameObject.renderer.material.mainTexture = clothe1;
         break;
     case 2:
         gameObject.renderer.material.mainTexture = clothe2;
         break;
     default:
         gameObject.renderer.material.mainTexture = clothe1;
         break;
 }

Avoid 6 deep nested ifs!

avatar image Lovrenc · May 31, 2014 at 04:36 PM 0
Share

Sometimes answering on unity answers is so frustrating. I'll give you a thumbs up for cold blood.

avatar image meat5000 ♦ · May 31, 2014 at 04:39 PM 0
Share

Much appreciated :)

avatar image

Answer by meat5000 · May 31, 2014 at 01:55 PM

I know it works in JS, not so sure about C#...

I use .ToString()

JS Example

 clone = Instantiate(nextBlockObject, newPlacement, Quaternion.Euler(0,0,turnAngle*nextPos));
         cloneCount += 1;
         clone.name = ("clone-"+nextBlockObject.name.ToString()+cloneCount.ToString());
Comment

People who like this

0 Show 12 · 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 thornekey · May 31, 2014 at 01:59 PM 0
Share

Cannot implicitly convert type string' to UnityEngine.Texture'

avatar image thornekey · May 31, 2014 at 02:01 PM 0
Share
     gameObject.renderer.material.mainTexture = ("clothe" + Top.ToString());

Top.ToString() is equal to 1

so it reads clothe1

which is

     public Texture2D clothe1;

but it doesnt like it being a string when the actual variable is a texture.

The whole reason is because Top can change up to 6 and there are 6 clothe1-6 variables...

avatar image meat5000 ♦ · May 31, 2014 at 02:03 PM 0
Share

I think the usage is different in C#

avatar image Lovrenc · May 31, 2014 at 02:06 PM 0
Share

@meat5000 ToString in JS and C# bot work pretty much the same. Problem is he is assigning string directly to texture property of material, instead of actually finding his the texture with that name first, and then assigning it.

avatar image thornekey · May 31, 2014 at 02:07 PM 0
Share

@Lovrenc, How do I assign it then?

Show more comments
avatar image

Answer by AlbertGp82 · May 31, 2014 at 02:20 PM

if i understand ... you cant create a name variable with string Have many options to do that, dictyonari,hashtable,Enums. or something like that can be more powerful and elegant but i can ,hope, get u an easy form.

mmmhhhh..

 public Texture2D clothe1;
 public Texture2D clothe2;
 public Texture2D clothe3;
 
 public Texture2D targetClothe;
 
 public void ChangeMainTexture(){
   
   int myTop = PlayerPrefs.GetInt("myTop")
 
   switch(myTop){
 
     case 1 :
          targetClothe =  clothe1;
       break;
     case 2 :
          targetClothe =  clothe2;
       break;
     case 3 :
          targetClothe =  clothe3;
       break;
 
   }
 
   gameObject.renderer.material.mainTexture = targetClothe;
 }
 

Can use string in switch if u want, or use Resources.Load() for load with string match, somethin like that

 gameObject.renderer.material.mainTexture = Resources.Load("/materials/textures/clothe" + Top,Texture);

Take a read about Resources folder and his access.This method its good idea if you do before gameplay,because have a heavy memory/procces impact,the other referencing in variables its better idea,ever you can, if you do during gameplay

Can be syntax error in myTop declaration, dont tried,im not a programmer C# :).Anyway you can explore more options to do that, you have many many ways.

Hope this works. Sorry for my english, its pretty pretty low cheers

Comment

People who like this

0 Show 3 · 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 thornekey · May 31, 2014 at 02:27 PM 0
Share

I dont understand how to implement that into my full code..

Heres a link if you would mind taking a look

http://pastie.org/private/sxz6kgba9yezntjd52zy8w

avatar image AlbertGp82 · May 31, 2014 at 02:39 PM 0
Share

i dont know the method networkView.RPC(),i never take a look at networks. But if u give a int value to getTop()

 public void getTop (int Top) {
          switch(Top){
  
 case 1 :
 targetClothe = clothe1;
 break;
 case 2 :
 targetClothe = clothe2;
 break;
 case 3 :
 targetClothe = clothe3;
 break;
  
 }
  
 gameObject.renderer.material.mainTexture = targetClothe;
 
     }
avatar image AlbertGp82 · May 31, 2014 at 02:47 PM 0
Share

or can do ...

     public void getTop (int Top) {
   
      
     gameObject.renderer.material.mainTexture = Resources.Load("/materials/textures/clothe" + Top,Texture);
      
     }

i dont remember if its -Texture or -Texture2D in Load().This is you have the textures in "local" folder in your assets project like Resources/material/textures, sure u can do that if u want take the texture of www, but i dont know exactly how,take a look unity help.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

How can i save a int for multiple texture. Please Read. 0 Answers

Assigning UV Map to model at runtime 0 Answers

Variable should have a value but if i start the game, the value resets to 0 1 Answer

Save/Load Variable with playerprefs 1 Answer

Screenshot to variable 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