Search Unity

Trying to create "responsive" quest log

Discussion in 'Unity UI (uGUI) & TextMesh Pro' started by jmfp92, Oct 11, 2019.

  1. jmfp92

    jmfp92

    Joined:
    Dec 15, 2017
    Posts:
    10
    Right now I am currently using a class to populate the cells of a grid layout with buttons onto a scroll view. The buttons have the name of the quest and are populating the gridlayout as I wanted. The problem I am having is when I add a button to the UI, I am trying to add a function via code that happens when I click that button. Upon doing the "StartQuest" function is when I add the quest button to the ui using another classes function Populate() which literally just instantiates the gameobject to the scroll view.

    Code (CSharp):
    1. public void StartQuest()
    2.     {
    3.         questActive = true;
    4.         Button uiButton = questUIForGrid.GetComponent<Button>();
    5.         questNameUI = uiButton.GetComponentInChildren<Text>();
    6.         questNameUI.text = questName;
    7.         //uiButton.onClick.AddListener(SetDescription);
    8.         GameObject thisQuestUI = uiButton.gameObject;
    9.         thisQuestUI.GetComponent<Button>().onClick.AddListener(SetDescription);
    10.         popQuestLog.Populate(thisQuestUI);
    11.     }
    The function I am trying to add to the button is the SetDescription, where I would simply activate another UI image with a text element as the child making this "appear" with a description of whatever quest you're on. I'm getting no nullreferences and assume this is just due to my ignorance of UI. PLEASE HELP! and let me know if you need more info, Thanks!

    Code (CSharp):
    1. public void SetDescription()
    2.     {
    3.         qMan.descriptionImage.SetActive(true);
    4.         qMan.descriptionText.text = questDescription;
    5.     }
    EDIT The main problem is the function isn't being added to the button click, although the quest name is working fine and I got most of the logic from the docs https://docs.unity3d.com/2018.2/Documentation/ScriptReference/UI.Button-onClick.html
     
    Last edited: Oct 11, 2019
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    1,239
    If uiButton is already a Button, why are you doing .gameObject.GetComponent<Button>()?

    I don't see anything obviously broken in your instantiation code. Try adding a Debug.Log message to SetDescription so you can verify whether it is failing to run or if it's running but doing the wrong thing. Also, make sure you are adding the SetDescription function from the correct object (when you just write "SetDescription", it means "this.SetDescription").
     
  3. jmfp92

    jmfp92

    Joined:
    Dec 15, 2017
    Posts:
    10
    @Antistone Because I was getting multiple buttons that would always change to the most recent Text as opposed to getting a new "instance" of a button per active quest that would contain that particular quests name on the UI, if that makes sense. I'm just testing for now so I'm sure that can be improved as well, but it will be chicken scratch for a while until I solve the main issue but the instantiating isn't the problem of the actual GameObject (button, that's working fine)

    Instead, It's mainly the function to the button issue, and that is the logic I wanted to use (this.SetDescription), so i'm assuming (since the class this is in is named Quest, and monobehavior inherit if that matters, doubt it) I should make an new instance of a Quest as well and assign the button to that (not around my pc to test)