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

Question I need help with my text based game

Discussion in 'Scripting' started by PanicMedia, Jul 21, 2021.

  1. PanicMedia

    PanicMedia

    Joined:
    Dec 6, 2020
    Posts:
    37
    I'm working on a text adventure game and for some reason the part of the script that is meant to separate the bits of text is being displayed as text in the game. is there anything I can do to stop this? This is the tutorial I'm going off:


    This is the script i think might be causing the problem:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class GameController : MonoBehaviour
    7. {
    8.     // Start is called before the first frame update
    9.  
    10.    public Text displayText;
    11.     [HideInInspector] public RoomNavigation roomNavigation;
    12.  
    13.     List<string> actionLog = new List<string>();
    14.     void Awake()
    15.     {
    16.         roomNavigation = GetComponent<RoomNavigation>();
    17.     }
    18.  
    19.   /// <summary>
    20.     /// Start is called on the frame when a script is enabled just before
    21.     /// any of the Update methods is called the first time.
    22.     /// </summary>
    23.     void Start()
    24.     {
    25.         DisplayRoomText();
    26.         DisplayLoggedText();
    27.     }
    28.  
    29.     public void DisplayLoggedText()
    30.     {
    31.         string logAsText = string.Join("/n", actionLog.ToArray());
    32.  
    33.         displayText.text = logAsText;
    34.     }
    35.  
    36.     public void DisplayRoomText()
    37.     {
    38.         string combinedText = roomNavigation.currentRoom.description + "/n";
    39.         LogStringWithReturn(combinedText);
    40.     }
    41.  
    42.     public void LogStringWithReturn(string stringToAdd)
    43.     {
    44.         actionLog.Add(stringToAdd + "/n");
    45.     }
    46.  
    47.     // Update is called once per frame
    48.     void Update()
    49.     {
    50.        
    51.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Do you mean actual C# code is being displayed at runtime as if the dialog was made of it? That sounds like perhaps you copied code into some file that is supposed to contain dialog perhaps?

    I am not going to do the tutorial above, but you can probably track down things pretty easily if you look for how the text gets introduced into things.

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
    PanicMedia likes this.
  3. PanicMedia

    PanicMedia

    Joined:
    Dec 6, 2020
    Posts:
    37
    I've fixed it, after doing some research I found out that I had the wrong slash in there it is supposed to be "\n" not "/n".