Switch from Printing to Debug.Log to Screen Text

Issue:
I currently have a script that prints to the debug log. I want it to stop printing to the debug log and print to the GUI text I have set up. The script that prints to the log is attached to a scene manager object that is called by a button press. So, whenever the button is pressed, it prints to the log.

Goal:
I’d like it to print to the screen in the GUI text instead when the button is pressed.

Scripts (I’m using C#):
Here is the part of the script that prints to the log. (This is from a larger script that is separate from and uses list elements that are also separate from the other two scripts I’ve included)

public void Greeting()
        { Debug.Log("Character is a " + gender + " " + age); }



    }

Here is the script for the text:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class UIController : MonoBehaviour
{

    public Text characterPrintText;
}

Here is the script for the button

using UnityEngine;
using System.Collections;

public class UIButton : MonoBehaviour {
    [SerializeField]
    private GameObject targetObject;
    [SerializeField]
    private string targetMessage;
    public Color highlightColor = Color.yellow;



    public void OnMouseOver()
    {
        SpriteRenderer sprite = GetComponent<SpriteRenderer>();
        if(sprite != null)
        {
            sprite.color = highlightColor;
        }
    }

    public void OnMouseExit()
    {
        SpriteRenderer sprite = GetComponent<SpriteRenderer>();
        if(sprite != null)
        {
            sprite.color = Color.white;
        }
    }
 public void OnMouseDown()
    {
        transform.localScale = new Vector3(5.9f, 5.9f, 5.9f);
    }
	
    public void OnMouseUp()
    {
        transform.localScale = new Vector3(6f, 6f, 6f);
        if (targetObject != null)
        {
            targetObject.SendMessage(targetMessage);
        }
    }
	}

If I’m understanding you correctly, you have a UI Text object in the scene and want to change its text component to display your Greeting() message?

If so:

public Text yourText;

public Greeting() {
    yourText.text = "Character is a....";
}

In this case I think you should simply make a static method that works the same way as Debug.Log (Debug.Log is a static method aswell). And sinse you need to change the text of something in the scene, you’ll have to be able to statically access your text field. For which you’ll need a singleton.

I would recommend looking at this tutorial, at minute 4:17 he starts talking about singleton, he explains it better then I can so that’s why I linked this tutorial :stuck_out_tongue:

In the code I post here I made some slight aleration to what’s public and private, but the core principle of a singleton remains the same.

Here is the code I would create:

public class UIController : MonoBehaviour
{
    private static UIController instance = null; 
    public Text characterPrintText;

    private void Awake()
    {
        if (instance == null)
            instance = this;
        else if (instance != this)
            Destroy(gameObject); 
    }

    public static void Log(string value)
    {
        instance.characterPrintText.text = value;
    }
}

And then to use this, simply use UIController.Log("Something")instead of Debug.Log("Something")and it should work. (Note that there needs to be 1 UIController somewhere in the scene and its characterPrintText must be set in the inspector, otherwise you’ll get nullreference exceptions).

Also, I think you should read a bit about a scope in C# (sorry, couldn’t find an easy beginner tutorial). Because reading your previous comments, it looks like you’re having trouble undestanding them.