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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

The object of type 'Transform' has been destroyed but you are still trying to access it.

Discussion in 'Scripting' started by nuereks, Nov 21, 2018.

  1. nuereks

    nuereks

    Joined:
    Nov 9, 2018
    Posts:
    9
    When I pick up my object and reach the destroy point, it also deletes my ability to control the object. I have a script that allows me to move the objects but it deletes it with the object. Is there any way I can make it ignore the other script?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ObjectFade : MonoBehaviour
    6.    
    7. {
    8.    
    9.     [SerializeField] private float fadePerSecond = 2.5f;
    10.     public GameObject Object = null;
    11.  
    12.     void Update()
    13.     {
    14.         if (Vector3.Distance(transform.position, Camera.main.transform.position) > 5.9)
    15.         {
    16.             var material = GetComponent<Renderer>().material;
    17.             var color = material.color;
    18.             material.color = new Color(color.r, color.g, color.b, color.a - (fadePerSecond * Time.deltaTime));
    19.             Destroy(gameObject, 1);
    20.             SetMaterialTransparent();
    21.         }
    22.     }
    23.     private void SetMaterialTransparent()
    24.  
    25.     {
    26.         foreach (Material m in Object.GetComponent<Renderer>().materials)
    27.  
    28.         {
    29.             m.SetFloat("_Mode", 2);
    30.             m.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
    31.             m.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
    32.             m.SetInt("_ZWrite", 0);
    33.             m.DisableKeyword("_ALPHATEST_ON");
    34.             m.EnableKeyword("_ALPHABLEND_ON");
    35.             m.DisableKeyword("_ALPHAPREMULTIPLY_ON");
    36.             m.renderQueue = 3000;
    37.  
    38.         }
    39.     }
    40. }
    41.  
     
  2. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    398
    It is because you are destroying it but then immediately after you are calling SetMaterialTransparent. Flip flop those last two lines.
     
  3. nuereks

    nuereks

    Joined:
    Nov 9, 2018
    Posts:
    9
    Nah those lines don't change anything whichever way they're placed. I've had this issue before I implemented the whole transparency thing.
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    Try
    Code (CSharp):
    1. Invoke("Destroy", 1);
    instead. Destroy stops gameobject from working on it immediately however destroys the object after the specified time AFAIK.