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

Bug World space canvas not rendering for unknown reason

Discussion in 'UGUI & TextMesh Pro' started by SMT5015, Jun 27, 2021.

  1. SMT5015

    SMT5015

    Joined:
    Apr 5, 2021
    Posts:
    53
    This is the code I made for teleporting door which works by player's command. If the player character is within the range, pressing a dedicated button transports him to the other door.

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using TMPro;
    4.  
    5. public class DoorPortalScript : MonoBehaviour
    6. {
    7.     [SerializeField] Transform otherDoor;
    8.     [SerializeField] Transform messagePos;
    9.  
    10.  
    11.     [SerializeField] Canvas canvas; //A world space canvas above the door
    12.     [SerializeField] TextMeshProUGUI message; //The text in this canvas
    13.     [SerializeField] string text;
    14.  
    15.     public PlayerInterface player;
    16.     public bool isAtThisDoor = false;
    17.  
    18.     WaitForSeconds enterTime = new WaitForSeconds(0.5f);
    19.  
    20.     void Start()
    21.     {
    22.         canvas.worldCamera = Camera.main;
    23.  
    24.         message.SetText(text);
    25.         message.gameObject.SetActive(false);
    26.     }
    27.  
    28.     void Update() //this is a workaround for trigger collision detection and teleportation
    29.     {
    30.         if(isAtThisDoor)
    31.         {
    32.             if(Input.GetAxisRaw("Vertical") == 1)
    33.             {
    34.                 if(player && !player.ScreenisBlack)
    35.                 StartCoroutine(player.BlackenScreen(1.5f));
    36.              
    37.                 StartCoroutine(EnterDoor());
    38.             }
    39.         }
    40.     }
    41.  
    42.    void OnTriggerEnter2D(Collider2D other)
    43.    {
    44.        if(other.gameObject.CompareTag("Player"))
    45.        {
    46.             player = other.gameObject.GetComponent<PlayerInterface>();
    47.             message.gameObject.SetActive(true);
    48.  
    49.             Debug.Log("Player detected by " + gameObject.name);
    50.             isAtThisDoor = true;          
    51.        }
    52.    }
    53.  
    54.    void OnTriggerStay2D(Collider2D other)
    55.    {
    56.         if(other.gameObject.CompareTag("Player"))
    57.         {
    58.             if(!player)
    59.             {
    60.                 player = other.gameObject.GetComponent<PlayerInterface>();
    61.                 Debug.Log("Player detected by " + gameObject.name);
    62.                 isAtThisDoor = true;
    63.             }
    64.         }
    65.    }
    66.  
    67.    void OnTriggerExit2D()
    68.    {
    69.        message.gameObject.SetActive(false);
    70.        isAtThisDoor = false;
    71.    }
    72.  
    73.    IEnumerator EnterDoor()
    74.    {
    75.         //otherDoor.GetComponent<DoorPortalScript>().player = player;
    76.         yield return enterTime;
    77.         player.gameObject.transform.position = otherDoor.position;
    78.         isAtThisDoor = false;
    79.         message.gameObject.SetActive(false);
    80.    }
    81. }
    The problem is, that the message (to indicate that this is and interactable object) does not render until the first interaction, i. e. starting the coroutine "EnterDoor()". Hierarchy, inspector and the scene screen of the Editor say everything works fine, but the game screen shows nothing. Also, rendering becomes normal after first using of any present door since entering play mode.

    I. e. if I use door 1 to move to door2, door2 is normal, then door1 is normal, and they both display their messages normally either if I enter by teleporting, or by walking into the trigger.

    This is not about TMP: I tried to add an image into the door's canvas and it was active all the time, but it still did not render. This is not about the canvas overlapping with the main one: IDK why, but I tested this. I don't know what happens into the coroutine which makes it change renderning.