Manage several sounds with one scripts

Hey everyone !!
First thank you reading this !! I’ve open this question beceaus i really need helps

Well, i’m working on a game with a group, i care about sounds and the thing we want to do it’s making an only script that offer specific methods wich the goal is macking songs. Like that, my group can use those methods on their script
But about question of optimizations, we want to do this : instead of creatin methods that taking AudioSource and AudioClip in parameters , we want that ALL the AudioSource and AudioClip we need be located on the script carring sound methods !
In this way it avoids to allocate memory by copying in memory parameters of methods, by using directly them into the script source of those methods

BUT !!! I’ve tryed again and again and i didn’t achieve to make this real , they tried to help me but in vain…
Here is really a way to handle this ? I’m now wondering if it’s not possible to do this x) (i’ve tried creating a new class by using heritage that contained all AudioSource and AudioClip we need, i’ve tried make method/class statics and i noticed that there was a mess of conflict in Unity with the ‘static’ key word, i’ve tried too load clip directly from folders, …)

So, i have to tell that i’ve often put the scripts that handle the AudioSource and AudioClip objects ‘variaables’ into a empty gameobject for allowing the “drag and drop” of AudioSource and AudioClip to the Scripts, but i start to think that we can’t do this and that we can only put AudioClip/AudioSource into a script IN the gameobject wich those Audioclip/AudioSource will affect …

SO ! Thanks for having reading this until here x) , i do not expect answers due to the specificity and nature of that question x) … But thank you so much if you answer me

Here is the ‘main’ code in question after removed ‘static’ key word and befor all the mess of the changes i’ve try to make it work
( and do not care about the ‘french’ comments if it disturbs you i’m sorry x) )

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sounds : MonoBehaviour

    private AudioSource AK47;
    private AudioClip AK47shootSound;
    private AudioClip AK47reloadSound;

    #region AK47

    #region membres
    private float timeSound = 0.001f; // temps du son de tir joué au fur et a mesur que l'on presse le bouton
    private float cadence = 0f; // permet de cadencer les tirs de l'AK K7
    #endregion membres

    #region AK47shoot
    public void AK47shoot(AudioSource AK47, AudioClip AK47shootSound,int nmbreDeMunitions = 1, float volume = 0.3f) // int nmbreDeMunitions = 1 et float volume = 0.3f son des membre només, pour les appeler il faut faire Sounds.AK47shoot(AK47, AK47shoot, munitions, volume);
        if (nmbreDeMunitions != 0) // Si il n'y a plus de munitions, alors le bruit de tir ne se fait plus car on ne sait plus tirer
            if ((Input.GetButton("Fire1")) && (timeSound >= cadence)) // permet de s'assurer que le boutton est résté appuyé, ainsi que de cadencer les tirs
                AK47.Stop(); // permet de jouer le son de la prochaine balle qui arrivera
                AK47.clip = AK47shootSound; // defini le son qu'emet l'AK47
                AK47.volume = volume; // defini le volume de l'AK47
                AK47.spatialBlend = 0.8f;
                AK47.Play(); // joue le son de l'AK47
                timeSound = timeSound + Time.deltaTime;  // permet de savoir le temps de son joué
                cadence = cadence + 0.100f; // Permet de initialiser la cadence a 1 tir tout les 0,1s, soit la candence réelle d'un AK47 :
            else if (Input.GetButton("Fire1")) // Permet l'empechement d'une boucle de son infinie
                timeSound = timeSound + Time.deltaTime;
            else if (!Input.GetButton("Fire1")) // permet de toujours initialiser ces variable a zero pour pouvoir retirer apres
                timeSound = 0.001f;
                cadence = 0f;
    #endregion AK47shoot

    #region AK47reload
    public void AK47reload(AudioSource AK47, AudioClip AK47reloadSound, float volume = 1f)
        AK47.clip = AK47reloadSound;
        AK47.volume = volume;

    #endregion AK47reload

    #endregion AK47
    #region Movement

    #region FootSteeps
    public void FootSteepsSound(AudioSource personnage)
        if (!personnage.isPlaying) personnage.PlayOneShot(personnage.clip);

    #endregion FootSteeps

    #region jump
    public void JumpSound(AudioSource piedPersonnage)
        if (!piedPersonnage.isPlaying) piedPersonnage.Play();
    #endregion jump

    #endregion Movement

Well I find the overall idea of concatening all sounds in one script rather overkill to be honest, since you will still have to attach an AudioSource to every object susceptible of making sounds anyways.
Then, you could maybe declare an array of AudioClips which will hold all of your possible AudioClips.
Afterwards you either assign in your “Sounds” script which Audio clip to play in which Audio source and when, or make every objects having an audio source take a reference of your Audio clip array and select his own to play in desired condition (You will need a script in the said object then).
Honestly again, better to just build a sound script for each object, I think the guys who made Unity are smart enough so this don’t take so many ressources to make your game lag :slight_smile: