- Home /
C sharp cant play Audio
So im on the point of going mad. Why doesnt this work!?
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.Characters.FirstPerson; [RequireComponent(typeof(AudioSource))]
public class MusicMode : MonoBehaviour {
 public UnityStandardAssets.Characters.FirstPerson.FirstPersonController controller;
 FirstPersonController fpc;
 public AudioClip runningOnE;
 private AudioSource audio;
 void Start()
 {
     audio = GameObject.Find("Player").GetComponent<AudioSource>();
     Track001();
 }
 void Track001()
 {
     audio.Play();
     audio.clip = runningOnE;
     audio.Play();
 }
}
Answer by Cornelius12 · Jun 16, 2019 at 04:50 PM
Also I cant remove the audiosource from the object i have the script attached to. Unity tells me then that the script depends on it. I dont understand why...
Answer by Doctor_ED · Jun 16, 2019 at 07:23 PM
First, you can't remove the AudioSource, because you have [RequireComponent(typeof(AudioSource))] on top of your script. Next, from what I can see, that script is also placed on "Player" prefab so you don't need to look for that Player. 
 Now here's a bit changed version of your script :
 using System.Collections; 
 using System.Collections.Generic; using UnityEngine; 
 using UnityStandardAssets.Characters.FirstPerson; 
     
 [RequireComponent(typeof(AudioSource))]    
 public class MusicMode : MonoBehaviour {
             
       public UnityStandardAssets.Characters.FirstPerson.FirstPersonController controller;
       FirstPersonController fpc;
       public AudioClip runningOnE;
       private AudioSource audio;
       void Start()
       {
               audio = GetComponent<AudioSource>();
               Track001();
        }
        void Track001()
        {
                audio.clip = runningOnE;
                audio.Play();
         }
 }
Try if it works, and also make sure that you are assigning actuall clip to your runningOnE in Inspector.
public class $$anonymous$$usic$$anonymous$$ode : $$anonymous$$onoBehaviour {
 public UnityStandardAssets.Characters.FirstPerson.FirstPersonController controller;
 FirstPersonController fpc;
 
 public GameObject runningOnEClip;
 
 public bool m_IsFocused;
 private crouchingScript roach;
 private void Start()
 {
     fpc = GameObject.FindObjectOfType<FirstPersonController>();
 }
 private void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.CompareTag("Track001"))
     {
         StartCoroutine(Track001());
     }
 }
 IEnumerator Track001()
 {
     runningOnEClip.SetActive(true);
     m_IsFocused = true;
     yield return new WaitForSeconds(282);
     runningOnEClip.SetActive(false);
     m_IsFocused = false;
 }
 private void Update()
 {
     if (m_IsFocused == true)
     {
         fpc.m_WalkSpeed = 7;
         fpc.m_RunSpeed = 9;
     }
     if (m_IsFocused == false)
     {
         fpc.m_WalkSpeed = 5;
         fpc.m_RunSpeed = 7;
     }
 }
}
NV$$anonymous$$ I found a solution
Thanks, will chek it out. Just found a workarounf with setActive gameobjects. Thanks i love u no homo
Your answer
 
 
             Follow this Question
Related Questions
How to stop music from restarting when reloading scene it began? 0 Answers
Unity Unable to Reassign Audio Clip 1 Answer
Stereo Mix as Input? 1 Answer
Can not play a disabled audio source 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                