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 slayer29179 · Aug 21, 2012 at 01:57 AM · stringchangeifvariable name

Change object texture using String [Java]

Hey all!

This question may have been answered before but it was never what I could implement into my script.

Basically I have a string which I have stored as NULL. Then I have 2 textures BlankIcon and WoodIcon. When the string is changed from NULL to either BlankIcon or WoodIcon then it will change another variable called TheNewItem's texture to either the BlankIcon texture or the WoodIcon texture. Here's my script so far;

 //String
 var NewItemName = "NULL";
 
 //Icons
 var BlankIcon : Texture;
 var WoodIcon : Texture;
 var TheNewItem : Texture;
 
 function Update()
 {
     if (NewItemName != "NULL")
     {
         TheNewItem = NewItemName; //This is the line I am unsure about.
     }
 }

That string I want to represent a variable name, so whatever the string is called if its a variable then it will assign it. I know you can do this with an If Statement but with the amount of items I will have this could be a very long script is there any easier way or do I just have to use if statements? Thank you for reading this! :)

Comment
Add comment · Show 4
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 mindlube · Aug 21, 2012 at 04:11 AM 1
Share

Slayer, if you check the scripting docs for $$anonymous$$aterial, there are some examples http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$aterial-mainTexture.html So do like if(NewItemName == "wood") { renderer.material.mainTexture = WoodIcon; } or something like that. However, don't do it in the Update function, only do it when the texture needs to change.

avatar image slayer29179 · Aug 21, 2012 at 09:48 AM 0
Share

That's what I was going to use but with the mass amount of textures I am going to be using I didn't want to have a whole script full of if statements but thank you! I will use that way then :)

avatar image Unity_gamer · Aug 21, 2012 at 12:32 PM 0
Share

Hi Slayer, Why can't you create a String array and store the Texture name and load according to your use?

avatar image slayer29179 · Aug 21, 2012 at 11:28 PM 0
Share

sorry for the late reply! was at college all day, I know how to create the string array

var Textures = String [];

but how do I load them accordingly?

1 Reply

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

Answer by Unity_gamer · Aug 22, 2012 at 06:43 AM

Add the String Names in the Editor(Make sure your Texture Name and the Given String name are Same). For Example If you want to load the Textures Randomly,

 var Random_numbr : int;
 
 Random_numbr = Random.Range(0,49);(If you have 50 Textures)


Make sure the Texture are in the Resource Folder and Use Resources.Load(Textures[Random_numbr]).(Texture[Random_number] holds String Name)

Hope this will Help you..:)

Thanks..

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 slayer29179 · Aug 22, 2012 at 02:02 PM 0
Share

Thank you! :D

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

if a string has certain letter(s) or number(s) 2 Answers

if,if,if, string cases, and if not, then something else? 1 Answer

Check if two strings are the same 2 Answers

Change the name of a toolbar button by clicking it. How? 1 Answer

The if does not work? 2 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