Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by RobbyT15 · Mar 18, 2016 at 10:21 PM · c#audiosourceaudioclip

AudioClip not playing

I have an audio clip that I'm trying to have start when a certain condition is met. The clip is being attached through the script I'm running which is supposed to be played using the AudioSource component attached to the GameObject. The clip is not playing, however. The variable holding the clip is a public AudioClip and it is being set within the inspector. Setting the AudioSource to play on awake would work at this point, but would not at a later time, so I want to avoid doing that. Does anyone have any suggestions?

C#

     using UnityEngine;
 using System.Collections;
 
 public class MoveCar:MonoBehaviour{
     //Car's idle speed
     private int idleSpeed = 0;
     //Car's current speed
     private float speed = 0.0f;
     //Car's top speed
     public float topSpeed = 165.0f;
     //Car's acceleration rate
     public float acceleration;
     //Car's braking power
     public float deceleration = 15f;
     private Rigidbody playerCar;
     private AudioSource engine;
     public AudioClip carStartAudio;
     private bool starting;
 
     void Start(){
         playerCar = GetComponent<Rigidbody>();
         engine = playerCar.GetComponent<AudioSource>();
         starting = true;
         if(starting){
             engine.clip = carStartAudio;
             if(!engine.isPlaying){
                 engine.Play();
             }
             Debug.Log(engine.clip);
         }
     }
 
     void Update(){
         if(Input.GetKey(KeyCode.W)){
             float horizontal = Input.GetAxis("Horizontal");
             float vertical = Input.GetAxis("Vertical");
             if(speed <= topSpeed){
                 speed = speed + (acceleration * Time.deltaTime);
                 Vector3 movement = new Vector3(vertical, 0f, horizontal);
                 playerCar.AddForce(movement * Mathf.Clamp(speed, idleSpeed, topSpeed));
                 Debug.Log(speed);
             }
             else{
                 speed = topSpeed;
                 if(speed == topSpeed){
                 }
             }
         }
     }
 }

If anyone would like to actually see what's going on, I've uploaded the game to my Dropbox, feel free to take a look.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ComradeVanti · Mar 19, 2016 at 02:32 PM 0
Share

First of all i would suggest you assign your audioclips directly to the array in the inspector. But thats not the main problem here.

Have you already checked (use a debug) if your audioclip variables get assigned correctly? Do you get any errors? Also, do you really want to play the sound each single frame? $$anonymous$$abye calling the source each frame cancels the sound it started playing in the privious frame... just an idea.

Anyways: Please return wirh more info.

avatar image RobbyT15 ComradeVanti · Mar 24, 2016 at 02:05 PM 0
Share

@Brullix3000 Sorry it took so long to get back to you. I have the audioclips assigned in the inspector and I am checking to see if they are being assigned correctly, they are. I also made the decision to focus on just one clip which should be playing in the start function, but isn't. I've made some changes to the project and the script and am updating my question to reflect that as well as added more details. I've also uploaded the game to my Dropbox so if you want to actually see what is actually happening, you're more than welcome to it.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

125 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Play audio after enemy dies 1 Answer

Second AudioClip won't play 0 Answers

How to make an editor extension to unity ui for switching audio clips? 0 Answers

[Got it!]Classes keep spitting out null values (trying to play a looped song) 2 Answers

Second AudioClip won't play 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges