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
2
Question by Prd_Animator · Dec 19, 2013 at 07:37 AM · textureing

how to change image texture using C#

hi,

i have an object with many textures and i want to change every texture with the single press of button, the problem is here i m very beginner of C# and don't know where to start i really don't know the basic of scripting, can anybody help or suggest me ??

any help would be appreciated

Thanks,

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by FatalError · Dec 19, 2013 at 03:34 PM

Hope this helps. I'm hardly the best, but this should work for your purpose. It'll grab the position, let your cycle through the whole array, and assigns it to your object's texture.

 public Texture[] myTextures = new Texture[5];
 int maxTextures;
 int arrayPos = 0;
 
 void Start ()
     {
         maxTextures = myTextures.Length-1;
     }
 
 
 void Update ()
     {
         if (Input.GetKeyDown(KeyCode.U))
         {
             renderer.material.mainTexture = myTextures[arrayPos];
 
             if(arrayPos == maxTextures)
             {
                 arrayPos = 0;
             }
             else
             {
                 arrayPos++;
             }
 
         }  
     }



Just attach the script to the object whose textures you want to change/cycle, and then drag & drop your textures to it in the editor. Doing it this way allows you to reuse this script on any object you want without having to recode anything.

Take note that if you're really wanting to change out their Material instead of the direct Texture, then you'll want to create your Material assets and then just change out Texture in this code with Material.

Example: public Material[] myMaterials = new Material[5];

Here is the code revised to change Materials instead of Textures.

     public Material[] myMaterials = new Material[5];
     int maxMaterials;
     int arrayPos = 0;
 
     void Start ()
     {
        maxMaterials = myMaterials.Length-1;
     }
 
 
     void Update ()
     {
        if (Input.GetKeyDown(KeyCode.U))
        {
          renderer.material = myMaterials[arrayPos];
 
          if(arrayPos == maxMaterials)
          {
           arrayPos = 0;
          }
          else
          {
           arrayPos++;
          }
 
        }  
 
     }

You can basically use this setup for whatever you're wanting to cycle with, just swap out the types. If you wanted to have a "previous" button, just make another section in the Update for that key and have it arrayPos--; instead.

If your default texture is also the first one in your array, you'll just want to put the renderer below the if check in update.

     void Update ()
     {
         if (Input.GetKeyDown(KeyCode.U))
         {
             if(arrayPos == maxMaterials)
                 arrayPos = 0;
             else
                 arrayPos++;
 
             renderer.material = myMaterials[arrayPos];
         }  
 
         if (Input.GetKeyDown(KeyCode.Y))
         {            
             if(arrayPos == 0)
                 arrayPos = maxMaterials;
             else
                 arrayPos--;
 
             renderer.material = myMaterials[arrayPos];
         }  
 
     }



You should go check out the Unity3D.com/learn site on their webpage. They've been updating it like crazy lately and it has a lot of excellent information there. There is a big section for the scripting also, and it goes over everything you'll need to know about C# to work with Unity.

C# Unity Scripting: http://unity3d.com/learn/tutorials/modules/beginner/scripting

Btw, it's bad practice to do what I just did in the Update (), you should make a method for it and call it in the Update instead. :D

     public Material[] myMaterials = new Material[5];
     int maxMaterials;
     int arrayPos = 0;
 
     void Start ()
     {
         maxMaterials = myMaterials.Length-1;
     }
 
     void updateMaterials()
     {
                 //Cycle forward
         if (Input.GetKeyDown (KeyCode.U)) {
                         if (arrayPos == maxMaterials)
                                 arrayPos = 0;
                         else
                                 arrayPos++;
             
                         renderer.material = myMaterials [arrayPos];
                 }
 
 
         
                 //Cycle backwards
         if (Input.GetKeyDown (KeyCode.Y)) {            
                         if (arrayPos == 0)
                                 arrayPos = maxMaterials;
                         else
                                 arrayPos--;
             
                         renderer.material = myMaterials [arrayPos];
                 }
     }
 
     
 
     void Update ()
     {
         updateMaterials ();
     }
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 Prd_Animator · Dec 26, 2013 at 05:31 AM 0
Share

@FatalError :Thanks for the writing a long and various scripts for me, i used that script and it works

 int Counter=0;
 void Start () 
 {

 }
 void Update () 
 {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.U))
     {
         Counter++;
         if(Counter>4)
             Counter=1;
         Texture txt=null;
         switch(Counter)
         {
         case 1:txt=Resources.Load("caution-signal", typeof(Texture2D)) as Texture;break;
         case 2:txt=Resources.Load("attention-signal", typeof(Texture2D)) as Texture;break;
         case 3:txt=Resources.Load("proceed-signal", typeof(Texture2D)) as Texture;break;
         case 4:txt=Resources.Load("stop-signal", typeof(Texture2D)) as Texture;break;
         }
         renderer.material.mainTexture =  txt;
     }  
 
 }

}

Thanks again guys,

avatar image
-1

Answer by Theinsanekiller · Dec 19, 2013 at 07:42 AM

hey , you can use this line of code to change your texture,

transform.renderer.material.SetTexture("_MainTex","texture");

as a texture you can use any texture you wish.

Comment
Add comment · Show 11 · 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 ani-gamer · Dec 19, 2013 at 08:24 AM 0
Share

thanks for reply, i will try

avatar image robertbu · Dec 19, 2013 at 08:30 AM 1
Share

SetTexture() will work for any texture property of a shader, but there is a 'shortcut' for the main texture:

 renderer.material.mainTexture = texture;
avatar image robertbu · Dec 19, 2013 at 10:01 AM 1
Share

Yeah but, if you do it with maintexture it will change for all the instances of that particular material

Not true. If you change a material property of 'renderer.material' at runtime, Unity creates a new material instance so that the change will only apply to the one object...even if the material is used in multiple places. You can use 'renderer.shared$$anonymous$$aterial' if you want to change all object that use the material.

avatar image Theinsanekiller · Dec 19, 2013 at 10:16 AM 0
Share

Yeah my mistake, and thanx a lot for correcting me....

avatar image Prd_Animator · Dec 19, 2013 at 10:48 AM 0
Share

@ roberthu : thanks for your comment,

using UnityEngine;

using System.Collections;

public class signal_change : $$anonymous$$onoBehaviour {

     GameObject c1;
     GameObject c2;
     // Use this for initialization
     void Start () {
         c1= GameObject.Find ("attention-signal");
         c2= GameObject.Find ("proceed-signal");

       }
 
       // Update is called once per frame
        void Update () 
 {
             //$$anonymous$$oving Left 
             if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.U))
                     renderer.material.mainTexture = c2;

         
 
 }

}

I m just using this scrit I don't know where i m worng and still facing problem.

Show more comments

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

How to size a texture for a object New to unity. Please Help 0 Answers

How to change texture of another object by clicking on a object? 0 Answers

Blender texture problem! 3 Answers

Flip a Texture? 3 Answers

Textures don's show? 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