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

Text color change C#

Discussion in 'Scripting' started by JohnBere, Aug 6, 2020.

  1. JohnBere

    JohnBere

    Joined:
    Jun 8, 2019
    Posts:
    2
    in my script, when I try to change the color of an item in the menu when selected (my menu works on arrows, not on the cursor, as is most often done) the color of the entire menu changes, not one item.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class MainMenuCon : MonoBehaviour
    7. {
    8.     public GameObject[] punktsMain;
    9.     public GameObject[] punktsPlay;
    10.     public GameObject[] menus;
    11.     public Text textNow;
    12.     public int menuID = 0;
    13.     public int punktID = 0;
    14.     private int menuLenght;
    15.     public GameObject punkNow;
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         menuLenght = punktsMain.Length;
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.  
    26.         textNow.GetComponent<Text>().material.color = Color.black;
    27.  
    28.     }
    in textNow I have only one menu item, which changes depending on what is selected, but despite this, the color of the entire menu changes.

    as you can see in the screenshot, the color of all the text turns black, but I'm trying to make it so that only the play button becomes black
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,179
    You want to set text.color instead of text.material.color.
     
    JohnBere likes this.
  3. JohnBere

    JohnBere

    Joined:
    Jun 8, 2019
    Posts:
    2
    Thank you so much, it really helped me.