Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Why is there no sound when I fire the projectile?

Discussion in 'Community Learning & Teaching' started by Asough, May 17, 2024.

Thread Status:
Not open for further replies.
  1. Asough

    Asough

    Joined:
    Sep 12, 2022
    Posts:
    177
    I'm trying to create a space shooting game, where the player has to shoot the asteroid.

    I want a sound effect to be played, whenever the player presses the spacebar to fire a projectile. So, I have tried to create a script, but it doesn't seem to work?

    Weirdly enough, it seems to work when I fire a projectile that doesn't hit the asteroid.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AudioManager : MonoBehaviour
    6. {
    7.     [Header("-------- Audio Source -------")]
    8.  
    9.     [SerializeField] AudioSource musicSource;
    10.     [SerializeField] AudioSource SFXSource;
    11.  
    12.     [Header("-------- Audio Clip ---------")]
    13.  
    14.     public AudioClip shoot;
    15.     public AudioClip music;
    16.  
    17.     private void Start()
    18.     {
    19.         musicSource.clip = music;
    20.         musicSource.Play();
    21.     }
    22.  
    23.     public void PlaySFX(AudioClip clip)
    24.     {
    25.         SFXSource.PlayOneShot(clip);
    26.     }
    27. }
    28.  
     
    Last edited: May 17, 2024
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,391
    What is calling
    PlaySFX()
    ?
     
  3. Asough

    Asough

    Joined:
    Sep 12, 2022
    Posts:
    177
    I'm not sure, I was following this tutorial.



    Also, would it be easier to work with if I made two scripts, one for music and one for sound effects?
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,391
    I'm not watching a 10 minute video to see where you went wrong. If you're saying "I'm not sure" then you need to go through it again and make sure you didn't skip that part.
     
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,682
    Oh, so that's one of the idiot tutorial creators that is introducing
    Find
    . If he's making similar choices throughout his tutorials I'm not at all surprised it's difficult to follow. If you have to use
    Find
    it's an indicator in almost every case that you've done something wrong.

    We need to see the classes that are trying to call
    PlaySFX
    .
     
    Last edited: May 17, 2024
    spiney199 likes this.
  6. Asough

    Asough

    Joined:
    Sep 12, 2022
    Posts:
    177
    I found the culprit. The problem was, that the projectile would hit the asteroid almost immediately after being fired and then disappear, before the sound effect could be played. I tried slowing down the speed of the projectile, and the sound effect plays now.
     
    Last edited: May 18, 2024
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,682
    Unity's
    Find
    methods are a newbie trap. They're extremely brittle (ie prone to mistakes) and they're very slow depending on which ones you're using. The sky is the limit with performance concerns. I've seen as slow as seconds on a 5950X (MSRP $799 USD) for a single call. Mobile (and most hardware) is far slower than my CPU.
     
    Last edited: May 26, 2024
  8. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,065
    If you have questions about a tutorial, contact the creator of the tutorial. Not here.
    Closed.
     
  9. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,065
    Ugh. As @Ryiah pointed out, that is a terrible "tutorial". Very bad practices. Use the official tutorials no randos from the internet.
     
    Ryiah and spiney199 like this.
Thread Status:
Not open for further replies.