Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to make a button that changes a texture of an object?

Discussion in 'Scripting' started by NirDafnai, Jul 20, 2014.

  1. NirDafnai

    NirDafnai

    Joined:
    Jul 18, 2014
    Posts:
    19
    Hey, I have a cube that when you press on it, it does something..
    and I have a ball and I want to change his texture by pressing on the cube.
    what do I need to write? I tried many scripts and they only changed the texture on the cube and not on the ball.
    I use javascript but if you must use c# then use it.
    Thanks :)

    (The 1,2,3,4 are the buttons or cubes that when they pressed they should change the texture of the ball).
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,937
    Can you show the code?

    Most likely you just need to add reference to the sphere:
    var mySphere;

    then assign the sphere into that at inspector,

    then you can change the material for sphere:
    mySphere.renderer.material.mainTexture = your_texture;
     
  3. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    I would recommend switching between materials rather than textures.

    You could attach a script similar to the following to each of your cube buttons:
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class SkinSelectorButton : MonoBehaviour {
    4.  
    5.     // Reference to the sphere object.
    6.     public GameObject sphere;
    7.     // The material that is to be selected.
    8.     public Material skinMaterial;
    9.  
    10.     private void OnMouseDown() {
    11.         sphere.renderer.sharedMaterial = skinMaterial;
    12.     }
    13.  
    14. }
    Associate your sphere object with each button along with the material associated with each button.

    I hope that this helps!
     
  4. NirDafnai

    NirDafnai

    Joined:
    Jul 18, 2014
    Posts:
    19
  5. NirDafnai

    NirDafnai

    Joined:
    Jul 18, 2014
    Posts:
    19
    I did what you do and I assigned the ball and the material and when i click on the cube nothing happens...
     
  6. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Did you assign the variables in the inspector?
     
  7. NirDafnai

    NirDafnai

    Joined:
    Jul 18, 2014
    Posts:
    19
    Yes:
     
  8. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    You did put the script on the buttons right?
    all 3 of them?
    with different materials assigned to 'SkinMaterial'?
     
  9. NirDafnai

    NirDafnai

    Joined:
    Jul 18, 2014
    Posts:
    19
    I put the script only on one to test it out.
     
  10. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Do your cube buttons have colliders attached?
     
  11. NirDafnai

    NirDafnai

    Joined:
    Jul 18, 2014
    Posts:
    19
    yes,

    Can I just send you the scene and you can check whats wrong with it?
     
  12. NirDafnai

    NirDafnai

    Joined:
    Jul 18, 2014
    Posts:
    19
    Please help me guys
     
  13. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Put the scripts on all 3 buttons.
    Now put 3 DIFFERENT materials as SkinMaterial.
     
  14. dagapublicity

    dagapublicity

    Joined:
    May 24, 2019
    Posts:
    1
    10000 thanks, great code, works perfect!!. I spend the whole day searching for the solution to this
     
  15. dknauss1

    dknauss1

    Joined:
    Feb 9, 2022
    Posts:
    2
    I'm pretty lost here trying to follow what to do. I'm doing something similar, I have 1 object, and I want to be able to change between 2 different materials for that object to render. I have two buttons. I wrote a script called solidView, and a script called transparentView. In the inspector I've set solidView to a "Button1", and transparentView to "Button2". Inside the inspector I've set the same object for each button. What next?
     
  16. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please share your code, using Code Tags.