Hey guys! I've been struggling with the Resources.Load method. Why? I wanted to pass a different sound depending on my GameObjects name load a different sound file. Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class LoadAudioClip : MonoBehaviour{ AudioSource audioSource; void Start(){ audioSource = GetComponent<AudioSource>(); audioSource.clip = Resources.Load<AudioClip>("Sounds/ObjectOne"); } } For now it just gets the file "ObjectOne" for every object. But lets say I have 3 GO: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class LoadAudioClip : MonoBehaviour{ private GameObject ObjectOne; private GameObject ObjectTwo; private GameObject ObjectThree; string nameOne, nameTwo, nameThree; AudioSource audioSource; void Start(){ nameOne = ObjectOne.name; nameTwo = ObjectTwo.name; nameThree = ObjectThree.name; audioSource = GetComponent<AudioSource>(); audioSource.clip = Resources.Load<AudioClip>("Sounds/[stringname]"); } } How could I pass the string into Resources.Load path?
Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class LoadAudioClip : MonoBehaviour{ string pathName; AudioSource audioSource; void Start(){ pathName = gameObject.name; audioSource = GetComponent<AudioSource>(); audioSource.clip = Resources.Load<AudioClip>("Sounds/Heart Sounds/${pathName}"); } } As I tried this way - it doesnt add the MP3 file into the Object's AudioSource with the same name. EDIT: Ofc I can't read, I put the $ in the wrong place, it should go like Code (CSharp): audioSource.clip = Resources.Load<AudioClip>($"Sounds/Heart Sounds/{pathName}"); It works now, ty.