Error and missing sound

Hi there,

I have a very nasty problem that I somehow can’t rid of. Every time my ship destroy an enemy who is controlled via Timeline, in the console appears the following error:

I think it has something todo with the Timeline, who wants to move the Enemy ship, but because it’s destroyed it can’t find it. But don’t know how to fix it.

The second bug I have is, that I can’t manage that the Explosion sound plays if the Enemy ship gets destroyed.

So does anyone have any idea what I can do?

Illya

Hi,

Unlikely to be Timeline, your error message does in fact state that you need to check your script.

Invariably you will be trying to do something with the GameObject that you have just destroyed, probably using Destroy(gameObject). Try to work backwards with this one, e.g. find the method which is responsible for the collision detection and contains the Destroy method and see what other code you have in there.

The audio issue is related, as you mentioned, you are destroying the GameObject you may have an AudioSource upon. You could instead use AudioSource.PlayClipAtPoint. It is a static method so it does not require an instance of the AudioSource component (thus you can remove it from your GameObject). When it is called it will create a new GameObject with an AudioSource component, play your AudioClip, and then destroy itself shortly afterwards.

Hope this helps :slight_smile:


See also;

Hello Rob,

thank you for your reply, but sadly my understanding of C# is still a little bit too lacking so that I can find the source of my error.
Below you can see my Enemy.cs script and I hope you can tell me I’m doing maybe wrong.
Note: The deathFX is an Empty GameObject with AudioSource in it that I attached to the Enemy=Ship which I wanna destroy.

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

public class Enemy : MonoBehaviour
{
    [SerializeField] GameObject deathFX;
    [SerializeField] Transform parent;
    [SerializeField] int pointsPerHit = 12;
    [SerializeField] int hits = 10;

    Collider boxCollider;

    ScoreBoard scoreBoard;

    // Start is called before the first frame update
    void Start()
    {
        AddBoxCollider();
        scoreBoard = FindObjectOfType<ScoreBoard>();
    }

    private void AddBoxCollider()
    {
        boxCollider = gameObject.AddComponent<BoxCollider>();
        boxCollider.isTrigger = false;
    }

    private void OnParticleCollision(GameObject other)
    {
        ProcessHit();
        if (hits <= 0)
        {
            KillsEnemy();
        }
    }

    private void ProcessHit()
    {
        scoreBoard.Scoring(pointsPerHit);
        hits = hits - 1;
    }

    private void KillsEnemy()
    {
        GameObject fx = Instantiate(deathFX, transform.position, Quaternion.identity);
        fx.transform.parent = parent;
        Destroy(gameObject);
    }
}

Thank you if you could help me out.

Illya

Hi Illya,

I’m sorry, I lost sight of your post.

Are you still experiencing this issue or did you manage to resolve it in the end?

Privacy & Terms