- Home /
Space Shooter Player Will Not Instantiate an Explosion After Getting Killed
I am a beginner to Unity and I am currently going through the Unity tutorial "Space Shooter" and am currently going through the adding hazards, enemies, etc portion. I found that through this process that it stopped Instantiating the explosion for the player and won't play the sound either. Hopefully I can get some help to fix this.
Answer by MochiTo · Oct 15, 2017 at 08:26 PM
There are too many ways that could make your game not working... you should be more specific in you questions in the future. i'll write down some potential problems I can think of...
haven't you accidentaly removed the tag "player" from your player?
is your DestroyByContact script in the right place?
is you destroyByContact script the same as it should be? as here at the bottom of the page: (scripting context)
does your player have the right collider set? isn't the collider disabled in the Inspector by accident on your player prefab?
is your player destroyed, but the explosion doesn't play? is the explosion still in the project? haven't you erased it by accident?
Sorry for the late response, but the player tag is set,, the destroyByContact script is in all the right places, and the script matches up with the tutorials with the enemy code included. The explosion sound and VFX is still there. The colliders are also set properly as well, still no instantiation. Not sure what the problem might be. Sorry I didn't specify these details in my question.
Answer by InvictusCo · Oct 15, 2017 at 08:58 PM
Could you post your code on here ( click the little button that says 101010 next to the paper clip) so that we can better see what your problem could be?
Answer by lordofworldz · Oct 22, 2017 at 03:54 AM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestoryByContact : MonoBehaviour
{
public GameObject explosion;
public GameObject playerExplosion;
public int scoreValue;
private GameController gameController;
void Start()
{
GameObject gameControllerObject = GameObject.FindWithTag("GameController");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent <GameController>();
}
if(gameController == null)
{
Debug.Log("Cannot find 'GameController' script.");
}
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag ("Boundary") || other.CompareTag ("Enemy"))
{
return;
}
if (explosion != null)
{
Instantiate(explosion, transform.position, transform.rotation);
}
if (other.CompareTag ("Player"))
{
Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
gameController.GameOver();
}
gameController.AddScore (scoreValue);
Destroy(other.gameObject);
Destroy(gameObject);
}
}
Answer by gwnguy · Oct 22, 2017 at 04:15 AM
I don't know if the mispelling of the class (DestoryByContact instead of DestroyByContact) (tory instead of troy) would cause a problem.
Are you getting any build/compile errors in the console.log?
Are you sure you haven't accidentally used/modified the code in the _Complete_Game directory?
And, as MochiTo says, confirm your tags ( GameController, Player, Enemy, Boundary. Existence, spelling AND punctuation)
So, I fixed the misspelling of the class, and that caused the hazards to not be detected, and I have checked all of the tags. The spelling and punctuation doesn't seem incorrect either, other than the DestoryByContact class name. There are also no compiling issues. Would you like me to send the rest of the Scripts?
Your answer

Follow this Question
Related Questions
Adding footsteps sounds to the Adventure tutorial 2 Answers
Detect the start of a particle burst emission 0 Answers
Play sound from particle 1 Answer
Sound play in spot of particle collision 1 Answer
Randomizing sounds (on keypress)? 1 Answer