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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

buttons not working (general UI)

Discussion in 'UGUI & TextMesh Pro' started by John_U, Apr 19, 2020.

  1. John_U

    John_U

    Joined:
    Feb 20, 2019
    Posts:
    9
    Hi,

    I was told my unity issue at present is a UI issue.

    I was following a video on chat logs and how to make them.



    and while I worked out my earlier issue which was I wasnt sure how to attach a script to the button, that is okay. My current problem is that the 1 buttons, red button, green button dont do anything. When I mousever the button it should change colour a little, yet it doesnt and when I press the button it should put a message in the chat log but also doesnt. If someone could please help, that would be great. Im a beginner to unity.

    The button doesnt seem to give an error, just doesnt do anything.

    upload_2020-4-19_12-17-51.png

    There are three main scripts (as per the video).
    RunLog.cs
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;  
    6.  
    7. public class RunLog : MonoBehaviour
    8. {
    9.  
    10.     [SerializeField]
    11.     private string myText;
    12.     [SerializeField]
    13.     private Color myColor;
    14.  
    15.     [SerializeField]
    16.     private TextLogControl logControl;
    17.  
    18.     public void LogText()
    19.     {
    20.         logControl.LogText(myText, myColor);
    21.     }
    22. }
    23.  
    TextLogItem.cs
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class TextLogItem : MonoBehaviour
    8. {
    9.  
    10.     public void SetText(string myText, Color myColor)
    11.     {
    12.         GetComponent<Text>().text = myText;
    13.         GetComponent<Text>().color = myColor;
    14.  
    15.     }
    16. }
    17.  
    TextLogControl.cs
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class TextLogControl : MonoBehaviour
    8. {
    9.     [SerializeField]
    10.     private GameObject textTemplate;
    11.  
    12.     private List<GameObject> textItems;
    13.  
    14.     void start()
    15.     {
    16.         textItems = new List<GameObject>();
    17.  
    18.     }
    19.  
    20.     public void LogText(string newTextString, Color newColor)
    21.     {
    22.         if (textItems.Count == 10)
    23.         {
    24.             GameObject tempItem = textItems[0];
    25.             Destroy(tempItem.gameObject);
    26.             textItems.Remove(tempItem);
    27.  
    28.         }
    29.  
    30.         GameObject newText = Instantiate(textTemplate) as GameObject;
    31.         newText.SetActive(true);
    32.  
    33.         newText.GetComponent<TextLogItem>().SetText(newTextString, newColor);
    34.         newText.transform.SetParent(textTemplate.transform.parent, false);
    35.  
    36.         textItems.Add(newText.gameObject);
    37.     }
    38. }
    Red button settings
    upload_2020-4-19_12-21-45.png

    TextLog settings
    upload_2020-4-19_12-22-39.png

    Viewport settings
    upload_2020-4-19_12-23-5.png
     

    Attached Files:

  2. John_U

    John_U

    Joined:
    Feb 20, 2019
    Posts:
    9
    Content Settings

    upload_2020-4-19_12-25-10.png

    Text settings

    upload_2020-4-19_12-25-32.png

    Any help greatly appreciated.

    I am slowly understanding to check the scripts very carefully and make sure no little red 'squiggles' showing code errors are present. So there are no red marks on the scripts.

    However I fail to understand why, as a initial point the button doesn't change to highlight color when I mouse-over as I changed the colors just to see if button was working.

    Secondly and most important I cannot get the button to place the message it is meant too, red button click is meant to say Warning in red letters in chat log, green button is meant to say normal in chat log.

    Thankyou,
     
  3. John_U

    John_U

    Joined:
    Feb 20, 2019
    Posts:
    9
    Hi,

    I found the solution to this, and I hope it helps someone else with same problem. The issue was no buttons at all were working. I found a post and it said your canvas should have a canvas group and graphic raycaster component, along with canvas component, rect transform, canvas scaler components.

    I think when I created the canvas it didn't add the graphic raycaster component. I added this component and all buttons now show clicking. My next step is to find out why the red warning text and normal green test is not being written into the chat log.

    One step at a time I guess :)

    I very much respect the people on this forum, I have only written a few post questions and everyone has been very helpful. Thankyou.
     
    xGaBe likes this.