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 Zubair1714 · Apr 15, 2014 at 08:33 AM · c#unityiphone

changing UITexture of A GameObject

Hi, I'm new to Unity so I don't know how to do the basic stuff in unity. How can I change the Texture of a GameObject? I've a png image, its name is stored in a string, and I want to change the Texture of my GameObject with that png image.

string dustTextureName = "Animal-Cat.png"; // name of the PNG image GameObject dustTexture; // public GameObject in which UITexture is set shown in Inspector dustTexture.GetComponent().mainTexture = dustTextureName; // this is the line where am stuck

Help me out.

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 robertbu · Apr 15, 2014 at 04:07 PM

You need to do one of two things. First you can deal with your texture directly. Do at the top of the file you would put.

 public Texture tex;

After attaching your script to a game object, select that game object and you will see the 'tex' variable in the inspector. You can click on the target symbol next to 'tex' and set the texture or you can drag and drop a texture onto the 'tex' variable. This will link this variable to a texture in your project. Then to change the texture in code, you just have to:

 renderer.material.mainTexture = tex;

The second solution is to put your texture in the Assets/Resources folder. It must be inside this folder to work. Then you can use Resources.Load() to load your image from a string:

 go.renderer.material.mainTexture = Resources.Load(dustTextureName, typeof(Texture2D));

While I don't think it is what you are asking, if you are trying to load the image from outside your Unity project (elsewhere on the machine or on the net), you can use the WWW class.

Comment
Add comment · Show 2 · 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 Zubair1714 · Apr 17, 2014 at 06:03 AM 0
Share

Thank you for replying but for this you must attach Renderer with you object, while the one that I cam using has none, anyways I solved it and will post the answer.

avatar image robertbu · Apr 17, 2014 at 06:39 AM 0
Share

You wrote:

How can I change the Texture of a GameObject?

This implies that you already had a texture on the object and therefore a Renderer component. You can as you indicate dynamically add a Renderer, but it might be cleaner and easier to setup a default renderer and material on the object and only change the texture at runtime.

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

20 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

Related Questions

A node in a childnode? 1 Answer

Object drags wherever I touch on screen- Iphone-Unity 1 Answer

Starting with unity - which scripting language ? 1 Answer

Distribute terrain in zones 3 Answers

Unity-Placing multiple objects in unity in form of grid/etc 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