How to get AudioClip from another GameObject?

I would like GameObjects to get a sound from a list of sounds on Start. The list of sounds is
in a script called gameController in a list:

public AudioClip unitCS1Audio;

I wrote this in the script that is trying to get the required audio:

var gameController = FindObjectOfType();
unitCS1Audio = gameController.unitCS1Audio[unitID];

Any yet, nothing happens and there is no error message. What am I doing wrong?

Modified metalted’s code to be more optimised. With this method you do not need to worry about finding the gameObject which your AudioLibrary is attached to.

 //This class should be added to an empty gameobject and added to a scene so we can access it
 //Lets for instance call this object "MyLibrary"
 public class AudioLibrary : MonoBehaviour {
          
      //This is a singleton
     public static AudioLibrary instance;
     //This is where your sounds go
     public AudioClip[] unitCS1Audio;
     
     private void Awake()
     {
          if (instance == null) instance = this;
     }
 }
 
 //This is the class that needs the audioclips, and can be attached to any object in the scene
 public class SomeAudioPlayer : MonoBehaviour {
     
     private void Start(){
           AudioClip[] unitCS1Audio = AudioLibrary.instance.unitCS1Audio;
     }
 }

Well, if there is an error message, it would be useful to include it in the question, so we can help you a bit better. If this is the actual code you are using, it is not going to find anything. The error message will probably be something like: there is no Type type provided as parameter in FindObjectOfType(), not really sure what the exact message is. If you want to find an object by type, you have to provide a type too. Next there is the AudioClip (which is not actually a list, but an array… just so you know, not important right now). So in your gamecontroller you have the AudioClip unitCS1Audio, so I’m assuming that in the other script, the unitCS1Audio will also be a AudioClip, but you are accessing an index from the array with the “[unitID]”. This will give you a single AudioClip, and not an AudioClip. So these are things to watch out for.


The following code shows an example of how to get variables from another script into a script of choice:

//This class should be added to an empty gameobject and added to a scene so we can access it
//Lets for instance call this object "MyLibrary"
public class AudioLibrary{
	
	//This is where your sounds go
	public AudioClip[] unitCS1Audio;
	
}

//This is the class that needs the audioclips
public class SomeAudioPlayer{
	
	public AudioClip[] unitCS1Audio;
	
	public void Start(){
		//We want to get the AudioClip[] from the AudioLibrary component on the MyLibrary object
		
		//So first we get the right gameobject
		GameObject myLib = GameObject.Find("MyLibrary");
		
		//Then we get the component from the gameobject
		AudioLibrary audioLib = myLib.GetComponent<AudioLibrary>();
		
		//Now that we have the right script, we can get the array from the script
		unitCS1Audio = audioLib.unitCS1Audio;
		
		//As it is not really needed to do everything on a new line, you could just write the following:
		unitCS1Audio = GameObject.Find("MyLibrary").GetComponent<AudioLibrary>().unitCS1Audio;
	}
}

How you get access to a gameobject is all up to you. You can find gameobjects by name, by tag, search through children or of course assign it in the inspector yourself. Getting the actual variable is the same in most cases, just get the right component from the object and get the variable, either directly when it is public or using the function when it is properly encapsulated. (The variable is private and uses a function to get the value)