Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

(Beginner) Problems with buttons at runtime (open/close text)

Discussion in 'UGUI & TextMesh Pro' started by strudi1986, Jun 21, 2015.

  1. strudi1986

    strudi1986

    Joined:
    Jun 21, 2015
    Posts:
    10
    Hi!
    I have some problems with buttons at runtime. I want to create a GUI where i can add some buttons at runtime if i find them ingame. When i click on a button, a text should appear on the right side. So far it works. But i want that the text is not visible until i click on the button. The other problem is that everytime i add a button to the GUI while a text (from another button) is open and if i click on a new button the text from the new button is not showing in a own window, it add the text to the text what is open. When i close one button and open a new one everything is alright. I hope you understand me, my english is not very well but i will add a image and the code, maybe you can see what my problem is. I worked many hours on this problem now, but i cant find the correct solution. Thank you!

    LogFileManager Script @EmptyGameObject

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class LogFileManager : MonoBehaviour {
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.  
    15.     }
    16.  
    17.     //Setup für die Logfileeinträge um sie während der Laufzeit an die richtige Position zu setzen und richtig zu skalieren etc.
    18.     public void LogFileSetup(string logFileFound, string logFileData, GameObject logEntryText, Button logFileFoundTitle,
    19.                              string logFileTitle, string entryText){
    20.            
    21.  
    22.         logFileFoundTitle.transform.SetParent(GameObject.Find(logFileFound).transform);
    23.         logEntryText.transform.SetParent(GameObject.Find(logFileData).transform);
    24.  
    25.         logFileFoundTitle.transform.localScale = new Vector3 (1,1,1);
    26.         logEntryText.transform.localScale = new Vector3 (1,1,1);
    27.  
    28.     }
    29.  
    30. }
    Test Script @Cube GameObject to Create a Button + Text in GUI (Button and Text are a Prefab, Logfile and LogfileText)

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class test : MonoBehaviour {
    7.  
    8.     public LogFileManager logFileManager;
    9.  
    10.     public Button logFileFoundTitlePrefab;
    11.     public GameObject logEntryTextPrefab;
    12.  
    13.     private bool isActive;
    14.    
    15.     // Use this for initialization
    16.     void Start () {
    17.  
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update () {
    22.    
    23.     }
    24.  
    25.     public void OnMouseDown(){
    26.         CreateLogFileEntry("LogfileButtons", "LogfileData", "Erster Eintrag", "Hallo ich bin ein Alien!");
    27.     }
    28.  
    29.    
    30.     //Erstelle einen neuen Logfileeintrag mit Prefabs
    31.     public void CreateLogFileEntry(string logFileFound, string logFileData, string logFileTitle, string entryText){
    32.        
    33.         GameObject logEntryText = Instantiate(logEntryTextPrefab) as GameObject;
    34.         Button logFileFoundTitle = Instantiate(logFileFoundTitlePrefab) as Button;
    35.        
    36.         logFileManager.LogFileSetup(logFileFound, logFileData, logEntryText, logFileFoundTitle, logFileTitle, entryText);
    37.        
    38.         logFileFoundTitle.onClick.AddListener(() =>  OpenLogEntry(logEntryText));
    39.        
    40.     }
    41.  
    42.     public void OpenLogEntry(GameObject logEntryText){
    43.         if(isActive == false){
    44.             HideEntryText(logEntryText);
    45.         } else
    46.             ShowEntryText(logEntryText);
    47.         }  
    48.    
    49.     public void HideEntryText(GameObject logText)  {
    50.         logText.SetActive(false);
    51.         isActive = true;
    52.     }
    53.    
    54.     public void ShowEntryText(GameObject logText)  {
    55.         logText.SetActive(true);
    56.         isActive = false;
    57.     }
    58.  
    59. }
    60.    
    61.  
     

    Attached Files:

  2. strudi1986

    strudi1986

    Joined:
    Jun 21, 2015
    Posts:
    10
    any ideas? cant find a solution.