Search Unity

Space Shooter Tutorial / NEED HELP

Discussion in 'Editor & General Support' started by Tbone21, Sep 23, 2017.

  1. Tbone21

    Tbone21

    Joined:
    Sep 22, 2017
    Posts:
    1
    I'm at the stage where I start adding sound and when I add the sound it just adds the audio clips to the hierarchy instead of the game object I've assigned it to. I think I've gotten that figured out but now that the music is playing the explosion visual effects for my player game object and my asteroid game object stopped working.

    Here is my code for my Destroy By Contact script:

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

    public class DestroyByContact : MonoBehaviour
    {
    public GameObject explosion;
    public GameObject playerExplosion;

    void OnTriggerEnter(Collider other) {
    if (other.tag == "Boundary")
    {
    return;
    }
    Instantiate(explosion, transform.position, transform.rotation);
    if (other.tag == "Player") {
    Instantiate (playerExplosion, other.transform.position, other.transform.rotation);
    }
    Destroy (other.gameObject);
    Destroy (gameObject);
    }
    }

    I don't believe I've done anything wrong, but if you see anything please let me know!
     
  2. Deleted User

    Deleted User

    Guest

    maby the prefab "playerExplosion" does not have an audio source that plays automatically, or the clip slot is empty.

    its best to address audio components directly..having different explosions sounds is nice too


    Code (CSharp):
    1. public AudioSource PlayerVoice; // sets where audio comes from
    2. public AudioClip[] Chatter;// sets the audio clip Array the Audio Source Plays.
    3. public float CommVolume = .45f; // sets the audio volume - you'll need this later = user volume level
    4.  
    5. //somewhere when you want to play audio clip
    6.  
    7. if ((PlayerVoice.isActiveAndEnabled) && (Chatter.Length > 0)) //is the audio source active and do I have a clip to play?
    8.             PlayerVoice.clip = Chatter[Random.Range(0, Chatter.Length)]; // now set a random clip from that clip array
    9.             if (!PlayerVoice.isPlaying)  // is the audio source already playing a clip? - no then
    10.                 PlayerVoice.Play(); // play the audio clip assigned above
    or just call a new method

    Code (CSharp):
    1.  
    2. if (other.tag == "Player")
    3.       iExplode(true);
    4. else
    5.      iExplode(false);
    6.  

    Code (CSharp):
    1.  
    2. private void iExplode(bool isPlayer){
    3. if (isPlayer)
    4.       Instantiate (playerExplosion, other.transform.position, other.transform.rotation);
    5. else
    6.       Instantiate(explosion, transform.position, transform.rotation);
    7.  
    8. if (Chatter.Length > 0) //is the audio source active and do I have a clip to play?
    9.             PlayerVoice.clip = Chatter[Random.Range(0, Chatter.Length)]; // now set a random clip from that clip array
    10.             PlayerVoice.Play(); // play the audio clip assigned above
    11. }
    12.  
    call the variables anything you want.

    hope that helps.

    p-