• 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 /
  • Help Room /
avatar image
Question by Zelentsov · Feb 06, 2016 at 10:01 PM · uichange materialselected

Change object texture on tap on UI Object

Somebody help me please! I have code:

     var matArray : Material[];
      private var index : int;
      
      function Update () {
           if (Input.GetButtonDown("Fire1")) {
             index++;
             index = index % matArray.Length;
             GetComponent.<Renderer>().material = matArray[index];
          }
      }

But I want change texture only then tap on UI object. How I can do it?

Thanks.

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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by rabby · Feb 11, 2016 at 06:06 AM

Hello @Zelentsov , If I understand correctly then you want to change the texture of materials when you touch/click on any UI object. For this do the following.

1.Create an empty Gameobject and attach a newly created c# script and paste the following code snippets on that script.

 public Material objectMaterial; // material which texture is going to change
 public Texture texture2change; // texture that will be reflected on material after change

 public void buttonClicked(){
     objectMaterial.mainTexture = texture2change;
 }

  1. Attach the material you want to change and the texture that will be reflected on material after change to the public field of the script.

  2. Create a new UI button/object , select that object on the hierarchy panel , on the inspector tab click ->Add Component -> UI -> Button(this step is necessary other that Button UI object).

  3. On the Onclick segment of button component, hit '+' sign , Add the newly created Gameobject(not the UI object , the other one) to the field indicating "None(Object)" , click "no function" section , select the script attached with the gameobject and then select the public method buttonClicked().

  4. Save and Run.

You have to attach the material to some gameobject in order to view the reflected change. Let me know if that helps you.

Thanks.

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 Pierce95 · Jun 16, 2016 at 07:40 AM 0
Share

Hi Rabby,

I'm looking for a similar thing to what Zelentsov has asked.

However this needs to work with the press of a specific UI button.

For example I will have a game object. There will be multiple UI buttons. The button will have a picture of the texture, When that button is pressed it will apply the shown texture to the object.

As a result I need atleast a dozen different buttons each which apply a different texture to the game object.

Do you have any suggestions?

avatar image rabby Pierce95 · Jun 16, 2016 at 12:11 PM 0
Share

public Material objectMaterial; // material which texture is going to change public void buttonClicked(Texture texture2change){
objectMaterial.mainTexture = texture2change; }

Attach the above code snippet to the gameobject and add the buttonClicked method as onclickListener of the button. Pass the texture as the parameter. In this way no matter how many button you create , you can pass different texture with each of them . Hope this solves your problem

avatar image Pierce95 rabby · Jun 20, 2016 at 09:50 AM 0
Share

Brilliant thanks for the example :)

Sorry for being unaware but I'm new to Unity and C#. I'm getting on well with it but have yet to do any real work with UI systems.

I will use the Snippet you have suggested and enter that into a new C# script. This can then be attached to my game object as a new component.

I understand that fine,

What I don't know how to do is associate this to the button.

"add the buttonClicked method as onclickListener of the button. Pass the texture as the parameter."

Not to sure how to actually do this part. Apologies if it is really simple and i'm just being naive.

I'm aware this was not originally my question and i'm going a little off topic so if you'd rather I messaged you privately you then that's fine.

I appreciate the assistance none the less :)

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.

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

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

Related Questions

Mobile game, Touchscript pass through UI 1 Answer

[Beginner] [UI text] Adapt the size of the text zone to the text inside 2 Answers

Multiplayer basic guidelines for a card game 2 Answers

BaseInputModule and custom navigation 0 Answers

I can't change Text color 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