Search Unity

Change the Color button works, but not completely

Discussion in 'Scripting' started by sonys222, Nov 19, 2015.

  1. sonys222

    sonys222

    Joined:
    Jun 3, 2015
    Posts:
    32
    Hello. When I touch a button, value of normal colors in button is change, but color of image is not change.After one touch, when i touch a second time but in other free spaces in my scenes the color of the image changes. Do I made an error in the function?

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class ZmianaTlaItemuwIS : MonoBehaviour {
    7.  
    8.     public int wybrany;    //variable that stores information about the selected object. Selected by touch.
    9.  
    10.     public  Button[] buttons=new Button[6];
    11.  
    12.     void Awake()
    13.     {
    14.         wybrany = PlayerPrefs.GetInt ("wybrany", 666);
    15.     }
    16.  
    17.     // Use this for initialization
    18.     void Start () {
    19.    
    20.        
    21.     }
    22.  
    23.  
    24.     // Update is called once per frame
    25.     void Update() {
    26.    
    27.         wybrany = PlayerPrefs.GetInt ("wybrany");
    28.         Debug.Log (wybrany);
    29.        
    30.         for(int i=0;i<=5;i++)
    31.         {
    32.            
    33.            
    34.             if(wybrany==i)
    35.             {
    36.                 ColorBlock cb= buttons[i].colors;
    37.                 cb.normalColor = new Color32 (189, 175, 175, 255);
    38.                 buttons[i].colors=cb;
    39.    
    40.                 if(i!=5)
    41.                 i++;
    42.             }
    43.            
    44.             ColorBlock cb1= buttons[i].colors;
    45.             cb1.normalColor = new Color32 (255, 255, 255, 255);
    46.             buttons[i].colors=cb1;
    47.  
    48.         }
    49.    
    50.     }
    51. }
     
  2. sonys222

    sonys222

    Joined:
    Jun 3, 2015
    Posts:
    32
    Someone help me with the code?
     
  3. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    which components are on your gameobject, an image and a button component ? or just a button component ?

    try also to change
    Code (CSharp):
    1. buttons[i].targetGraphic.color
    also it doesn't depend on your touches, because you have everything in your update method it's called every frame anyway

    better would be a method that just is called ones and sets the colors to the buttons you want
     
    Last edited: Nov 20, 2015
  4. sonys222

    sonys222

    Joined:
    Jun 3, 2015
    Posts:
    32
    In button script. Script for touch looks like that. So if player touches a button and He chooses the image ,button to change to make it clear that the object is selected. Here is a picture of how it all looks http://postimg.org/image/5r7d3gfm3/

    Code (csharp):
    1.  
    2.    void Update()
    3. {
    4. for (var i = 0; i < Input.touchCount; ++i) {
    5.             Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch (i).position);
    6.          
    7.             if (Input.GetTouch (i).phase == TouchPhase.Began) {
    8.  
    9.              
    10.                 if (Physics.Raycast (ray, out hit))
    11.                  
    12.                     if(hit.collider.gameObject.tag=="car" )
    13.                 {
    14.                     if(kredki>=price&&carkupiony!=1)
    15.                     {
    16.                         car.GetComponent<Image> ().sprite = item1;
    17.                         crayons=crayons-price;
    18.                         PlayerPrefs.SetInt("car",1);     // buy images
    19.                         PlayerPrefs.SetInt ("wybrany",0);  // Selecting the current background image to the game, "0" means car
    20.                         PlayerPrefs.SetInt("CrayonsPoints",crayons);
    21.  
    22.                     }
    23.                 if(carkupiony==1)
    24.                     {
    25.                         PlayerPrefs.SetInt ("wybrany",0);
    26.              
    27.                     }
    28.                 }
    29. }
    30.  
     
    Last edited: Nov 20, 2015
unityunity