Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by Ploopy128 · Jul 12, 2020 at 03:50 AM · script.soundsound effectssfx

How to play multiple audio files from one script

I have been working on a platformer, and I am trying to have both a jump sound play (already have it) and a coin sound play (don't have this one) my code is bellow, what do I have to do? I have tried a lot of things, so the coin sound is not implemented below right now. using System; using System.Collections; using System.Collections.Generic; using System.Threading; using UnityEngine; public class PlayerMovement : MonoBehaviour { public CharacterController2D controller; public Animator animator; public float runSpeed = 40f; AudioSource jumpSound; float HorizontalMove = 0f; bool Jump = false; bool Crouch = false; void Start() { jumpSound = GetComponent<AudioSource>(); } // Update is called once per frame void Update() { HorizontalMove = Input.GetAxisRaw ("Horizontal") * runSpeed; animator.SetFloat("speed", Mathf.Abs(HorizontalMove)); if (Input.GetButtonDown("Jump")) { Jump = true; jumpSound.Play(); animator.SetBool("isJumping", true); } if (Input.GetButtonDown("Crouch")) { Crouch = true; } else if (Input.GetButtonUp("Crouch")) { Crouch = false; } } public void onLanding() { animator.SetBool("isJumping", false); } public void onCrouching(bool IsCrouching) { animator.SetBool("isCrouching", IsCrouching); } void FixedUpdate() { //move our charactor controller.Move(HorizontalMove * Time.fixedDeltaTime, Crouch, Jump); Jump = false; } void OnTriggerEnter2D(Collider2D other) { if (other.gameObject.CompareTag("Coin")) { Destroy(other.gameObject); } } }

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by finnjwohner · Jul 12, 2020 at 04:46 AM

You could have 2 AudioSources on the same object and just assign those to variables in script like this;

 private AudioSource jumpSound;
 private AudioSource coinSound;
 
 private void Start() {
     jumpSound = GetComponents<AudioSource>()[0];
     coinSound = GetComponents<AudioSource>()[1];
 }

And then just invoke Play() whenever you want to play either sound. Keep in mind if you do this then the order of the AudioSources matters in the inspector (Jump sound audio source should come first, then coin sound).

Comment
Add comment · Show 1 · Share
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 Ploopy128 · Jul 12, 2020 at 06:53 PM 0
Share

thank you soo much, it works!

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

163 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 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

How do I make enemies play a sound when they die? 1 Answer

Optimised audio system design for mobile 0 Answers

How to create impact sounds? 3 Answers

Audio Crackling in Unity 4 - Possibly related to adjusting volume at run-time 0 Answers

Unity plays only a few gameobject's sound 2 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