Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

[Solved] Can't destroy Transform

crizzydimecrizzydime Member
edited July 2020 in Programming

Hello Everyone, I'm currently following the 2D platformer Tutorial on YouTube and ran into a roadbloack. Everything was working fine until I got to video 20!

The enemy and particles are no longer being destroyed. I'm getting this error in Unity

"Can't destroy Transform component of 'AlienSpaceshipDeathParticles(Clone)'. If you want to destroy the game object, please call 'Destroy' on the game object instead. Destroying the transform component is not allowed."

I believe the issue lies within the GameMaster script I posted below. If you'd like to see anymore of my scripts, please ask. This video worked with 2 other scripts including this one.


Thanks for all the help! :)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class GameMaster : MonoBehaviour
{


    public static GameMaster gm;


    void Awake()
    {
        if (gm == null)
        {
            gm = this;
        }
    }


    public Transform playerPrefab;
    public Transform spawnPoint;
    public float spawnDelay = 2;
    public GameObject spawnPrefab;
    public AudioClip respawnAudio;


    public CameraShake cameraShake;


    void Start()
    {
        if (cameraShake == null)
        {
            Debug.LogError("No camera shake referenced");
        }
    }




    public IEnumerator RespawnPlayer()
    {
        GetComponent<AudioSource>().Play();
        yield return new WaitForSeconds(spawnDelay);
        Instantiate(playerPrefab, spawnPoint.position, spawnPoint.rotation);
        GameObject clone = Instantiate(spawnPrefab, spawnPoint.position, spawnPoint.rotation) as GameObject;
        Destroy(clone, 3f);
        
    }
    public static void KillPlayer(Player player)
    {
        Destroy(player.gameObject);
        gm.StartCoroutine(gm.RespawnPlayer());
    }


    public static void KillEnemy(Enemy enemy)
    {
        gm._KillEnemy(enemy);
    }
    public void _KillEnemy(Enemy _enemy)
    {
       GameObject _clone = Instantiate(_enemy.deathParticles.gameObject, _enemy.transform.position, Quaternion.identity) as GameObject;
        Destroy(_clone.gameObject, 5f);
        cameraShake.Shake(_enemy.shakeAmt, _enemy.shakeLength);
        Destroy(_enemy.gameObject);
        
    }
}


Best Answer

Sign In or Register to comment.