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

Type of object "Text" has been destroyed

Discussion in 'Scripting' started by Rebunkee, Nov 4, 2020.

  1. Rebunkee

    Rebunkee

    Joined:
    Jan 7, 2018
    Posts:
    3
    Hi! I was wondering if somebody could help me. I'm fairly new to unity and C#, and I'm working on a dummy project to learn how everything works. I've run into a problem, I'm working on dialogue and giving full control back when the dialogue is finished. This works, BUT I wanted to learn to re-engage in with the NPC, but now the text that shows "Press E to Interact" keeps showing even without looking at the NPC and pressing E on the NPC all of a sudden does nothing. I also get "The object of type 'Text' has been destroyed but you are still trying to access it." Whilst the GameObject AND text still exist in the scene. I've checked if the update() function still runs with debug, and I've found out that it stopped 'updating'. Anyone have any idea what exactly I'm doing wrong? Please keep in mind that I'm a novice!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6.  
    7. public class InteractScript : MonoBehaviour
    8. {
    9.     private Camera cam;
    10.     public float distance = 1f;
    11.     private GameObject Player;
    12.     private GameObject TwineTextPlayer;
    13.  
    14.     public GameObject twineplayer;
    15.     public GameObject ContCanvas;
    16.     private GameObject twineplayerClone;
    17.     private GameObject ContCanvasClone;
    18.     Text messageText;
    19.  
    20.  
    21.     public bool seesNPC = false;
    22.     public bool buttonpress = false;
    23.     private void Start()
    24.  
    25.     {
    26.         messageText = GameObject.Find("CanvasE/Text").GetComponent<Text>();
    27.     }
    28.     private void Update()
    29.     {
    30.         Player = GameObject.Find("Player");
    31.         TwineTextPlayer = GameObject.Find("TwineTP");
    32.         FPS_Controller fpscontroller = Player.GetComponent<FPS_Controller>();
    33.         messageText.text = "";
    34.         if(seesNPC == false){
    35.             messageText = GameObject.Find("CanvasE/Text").GetComponent<Text>();
    36.             messageText.text = "";
    37.             cam = Camera.main;
    38.             Vector3 origin = cam.transform.position;
    39.             Vector3 direction = cam.transform.forward;
    40.             float distance = 4f;
    41.             RaycastHit hit;
    42.             if (Physics.Raycast(origin, direction, out hit, distance)){
    43.                 if (hit.transform.tag == "NPC"){
    44.                     messageText.text = "Press E to interact";
    45.                     if (Input.GetKeyDown(KeyCode.E)){
    46.                         Debug.Log("seesnpc2");
    47.                         seesNPC = true;
    48.                         Instantiate(twineplayer);
    49.                         Instantiate(ContCanvas);
    50.                         messageText.text = "";
    51.                         fpscontroller.dialoguestart = true;
    52.                     }
    53.                 }
    54.                 else{
    55.                     messageText.text = "";
    56.                 }
    57.             }
    58.  
    59.         }
    60.         Start();
    61.     }
    62.  
    63.     public void ClearTextPlayer()
    64.     {
    65.         Player = GameObject.Find("Player");
    66.         twineplayerClone = GameObject.Find("TwineTP(Clone)");
    67.         Destroy(twineplayerClone);
    68.         ContCanvasClone = GameObject.Find("ContCanvas(Clone)");
    69.         Destroy(ContCanvasClone);
    70.         FPS_Controller fpscontroller = Player.GetComponent<FPS_Controller>();
    71.         fpscontroller.dialoguestart = false;
    72.         fpscontroller.NinDialoge = true;
    73.         seesNPC = false;
    74.         if(seesNPC == false)
    75.         {
    76.             Debug.Log("Clear");
    77.         }
    78.         //Start();
    79.         Update();
    80.     }
    81.  
    82.        
    83. }
    84.  
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Sounds like the Text component you are trying to modify (or, more likely, the game object it was attached to) got destroyed at some point, so it is no longer in your scene.

    Unity objects are generally only destroyed if you load a new scene or if you have a script that explicitly destroys them by calling Destroy() or DestroyImmediate().

    I don't know how your dialogue system works, but I would guess that when a dialogue is "finished", it destroys one or more objects that it was using to show the dialogue. If the system is well-designed, the objects it is destroying were probably created at the start of the dialogue, and so you probably just need to create them again as if starting a new dialogue from scratch.
     
  3. Rebunkee

    Rebunkee

    Joined:
    Jan 7, 2018
    Posts:
    3
    Thanks for taking the time to look at my 'mess'! It does destroy the dialogue component, however the error is referencing a Text that is not a child of this dialogue system. I don't delete it in any script, nor do I load any other scenes.
    Last night, when I couldn't sleep, I thought of a solution which might solve this. I'll use GameObject.Enabled instead of Destroy since i'll be using the dialogue system multiple times. I'll post any updates on here, for anyone having the same problem.

    This doesn't explain how I got the error though. As I'm using this project as a learning tool, it would be great to know why I get the error.
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Figure out where the reference came from that Unity is telling you has been destroyed, trace it backwards in time, try to find a time before it got destroyed. Figure out where it is in the object hierarchy. Trace it forwards again and figure out when it gets destroyed.