Search Unity

Button should change his texture seven times by clicking it

Discussion in 'Scripting' started by LucaKawe, May 28, 2017.

  1. LucaKawe

    LucaKawe

    Joined:
    May 28, 2017
    Posts:
    4
    Hey guys
    can somebody pls help me? I tryed for 3 hours to find or to creat a solution but I failed (C#).
    I tryed to creat this:



    Thanks for your help.
     
  2. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    What have you tried so far?
     
  3. LucaKawe

    LucaKawe

    Joined:
    May 28, 2017
    Posts:
    4
    public class TexChanger : MonoBehaviour {

    int a; // a = Count from 1 to 7

    public void Button_Click() {
    if(Input.GetButtonDown(a + 1)
    Debug.Log(a);

    if(a==1) {
    renderer.material.mainTexture1
    }
    if (a == 2)
    {
    renderer.material.mainTexture2
    }
    if (a == 3)
    {
    renderer.material.mainTexture3
    }
    if (a == 4)
    {
    renderer.material.mainTexture4
    }
    if (a == 5)
    {
    renderer.material.mainTexture5
    }
    if (a == 6)
    {
    renderer.material.mainTexture6
    }
    if (a == 7)
    {
    renderer.material.mainTexture7
    }

    return;
    }
     
  4. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    You're not assigning "a" anywhere.
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    if your Button_Click() is connected to the OnClick event of the button in the inspector, that part is correct. If not, that's what you should do.
    You should remove "If(Input.GetButtonDown(a+1))" - just get rid of that.
    And then, as the previous poster said. Make sure you begin by assigning a value to 'a'.. and don't forget to put keep it within your desired range. :)
     
  6. LucaKawe

    LucaKawe

    Joined:
    May 28, 2017
    Posts:
    4
    I don't get it :( I'm a beginner and it's very complicated for me :'(

    using UnityEngine;
    using System.Collections;

    public class RundenzahlerTexWechler : MonoBehaviour {

    int a = counter;
    public a = 1;

    ///count the number of click
    counter++;

    if (a == 1)
    renderer.material.mainTexture1;

    if (a == 2)
    renderer.material.mainTexture2;

    if (a == 3)
    renderer.material.mainTexture3;

    if (a == 4)
    renderer.material.mainTexture4;

    if (a == 5)
    renderer.material.mainTexture5;

    if (a == 6)
    renderer.material.mainTexture6;

    if (a == 7)
    renderer.material.mainTexture7;

    if (counter > 7)
    return;

    }
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    If you're that new, you'd likely benefit from some introductory tutorials for Unity/scripting in Unity.
    Check out some on the website.
    If this is a UI button, you can just change the sprite (pretty sure you don't need the renderer material).

    There are several other issues with your script, but I would just be giving you bits of a tutorial here.

    You script doesn't have a function.
    your 'a' value isn't declared properly, I figure you meant: public int a = 1;
    and 'counter' isn't necessary since it's doing the exact same thing as 'a'.

    Please try to start at the beginning. I promise you you'll have a much nicer time. :)
     
    aer0ace likes this.