Question by
nreed132 · Jan 24, 2020 at 11:11 PM ·
gameobjectdestroy
Why is this object not destroying itself?
It's an instance of a prefab, but it's not destroying itself when I tell it to. Here's the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class AIRandomMove : MonoBehaviour
{
public float radius;
AudioSource m_AudioSource;
float timer;
public MusicPlayer other;
Transform target;
NavMeshAgent enemy;
public float wanderTimer;
public GameObject explosion;
// Start is called before the first frame update
void Start()
{
enemy = GetComponent<NavMeshAgent>();
timer = wanderTimer;
m_AudioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
timer += Time.deltaTime;
if (timer >= wanderTimer)
{
Vector3 newPos = Random.insideUnitSphere * radius;
enemy.SetDestination(newPos);
timer = 0;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Projectile")
{
Explode();
Destroy(gameObject);
}
}
void Explode()
{
m_AudioSource.Play();
GameObject exploder = Instantiate(explosion, transform.position, transform.rotation);
exploder.GetComponent<ParticleSystem>().Play();
ScoreManager.score += 1;
other.playExplosion();
}
}
When I tell it to call other.playExplosion() it calls this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MusicPlayer : MonoBehaviour
{
public AudioClip soundSource;
public AudioSource main;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void playExplosion()
{
if(!main.isPlaying)
{
main.Play();
}
}
}
Most likely some stupid mistake, as i'm new. If you see it please point it out!
Comment