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 /
avatar image
0
Question by Patito00 · Mar 25, 2020 at 01:00 AM · audiosourceaudio.playoneshot

I can't play a sound with AudioSource.PlayOneShot()

Hi!

I want to play a sound, but it doesn't work. I can't hear the sound. Here I give you the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Coin : MonoBehaviour
 {
     [SerializeField] private Vector3[] randomPositions = new Vector3[3];
     [SerializeField] private AudioClip dead_clip;
 
     public void Restart(int pos)
     {
         GetComponent<AudioSource>().PlayOneShot(dead_clip, 1);
 
         gameObject.SetActive(true);
         transform.localPosition = randomPositions[pos];
     }
 }

I use Restart void here:

 void OnTriggerEnter(Collider col)
     {
         // checking if the object is a coin
         if (col.CompareTag("Coin"))
         {
             points++;
             col.gameObject.SetActive(false);
 
             // checking if all the balls are disabled
             bool inactiveAllCoins = true;
 
             for (int i = 0; i < Coin_parent.childCount; i++)
             {
                 if (Coin_parent.GetChild(i).gameObject.activeSelf)
                 {
                     inactiveAllCoins = false;
                     return;
                 }
             }
 
             if (inactiveAllCoins)
             {
                 int randomPos = Random.Range(0, 2);
                 foreach (Transform coin in Coin_parent)
                 {
                     Coin coin_script = coin.gameObject.GetComponent<Coin>();
                     coin_script.Restart(randomPos);
                 }
             }
         }
     }

Thanks for reading and answer me if you can. I'd be great for me. Good luck! ;)

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
0

Answer by Patito00 · Mar 27, 2020 at 08:08 PM

Guys, I have finished it. I just modify the Restart void like this, adding a new AudioClip killing_coin_clip:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class Player : MonoBehaviour
 {
 
     // varaibles
     [SerializeField] private float speed;
     [SerializeField] private GameController gameController;
     [SerializeField] private AudioClip killing_coin_clip;
     private Vector3 self_rotation = new Vector3();
     private int points;
     private int record;
 
     Rigidbody m_Rigidbody;
     Text Point_txt;
     Text Record_txt;
     Transform Coin_parent;
     AudioSource audioSource;
 
     void Start()
     {
         // getting components and PlayerPrefbs
         m_Rigidbody = GetComponent<Rigidbody>();
         Point_txt = GameObject.Find("Points text").GetComponent<Text>();
         Record_txt = GameObject.Find("Record text").GetComponent<Text>();
         Coin_parent = GameObject.Find("Coins").transform;
         audioSource = GetComponent<AudioSource>();
         audioSource = GetComponent<AudioSource>();
         audioSource = GetComponent<AudioSource>();
 
         record = GetRecord();
     }
 
 
     void Update()
     {
         // moving the player
 
         /* aca pude haberlo hecho mas entendible. La proxima, puedo hacer un mejor codigo*/
         if (Input.GetKey(KeyCode.UpArrow) && Time.timeScale > 0)
         {
             self_rotation.y = 0;
         }
         else if (Input.GetKey(KeyCode.DownArrow) && Time.timeScale > 0)
         {
             self_rotation.y = 180;
         }
         else if (Input.GetKey(KeyCode.LeftArrow) && Time.timeScale > 0)
         {
             self_rotation.y = 270;
         }
         else if (Input.GetKey(KeyCode.RightArrow) && Time.timeScale > 0)
         {
             self_rotation.y = 90;
         }
         transform.localEulerAngles = self_rotation;
         transform.Translate(new Vector3(0f, 0f, speed) * Time.deltaTime);
 
         // changing to pause state
         if (Input.GetKeyDown(KeyCode.Space))
         {
             gameController.Pause();
         }
 
         // setting texts
         Point_txt.text = "Points: " + points;
         Record_txt.text = "Record: " + record;
 
         // getting record of the player       
         if(points > record)
         {
             SetRecord(points);
             record = GetRecord();
         }
     }
 
     void OnCollisionEnter(Collision col)
     {
         // enemy collision event
         if (col.gameObject.tag == "Enemy")
         {
             gameController.GameOver();
         }
     }
 
     void OnTriggerEnter(Collider col)
     {
         // checking if the object is a coin
         if (col.CompareTag("Coin"))
         {
             points++;
             audioSource.PlayOneShot(killing_coin_clip, 1);
             col.gameObject.SetActive(false);
 
             // checking if all the balls are disabled
             bool inactiveAllCoins = true;
 
             for (int i = 0; i < Coin_parent.childCount; i++)
             {
                 if (Coin_parent.GetChild(i).gameObject.activeSelf)
                 {
                     inactiveAllCoins = false;
                     return;
                 }
             }
 
             if (inactiveAllCoins)
             {
                 int randomPos = Random.Range(0, 2);
                 foreach (Transform coin in Coin_parent)
                 {
                     Coin coin_script = coin.gameObject.GetComponent<Coin>();
                     coin_script.Restart(randomPos);    
                 }
             }
         }
     }
 
     private int GetRecord()
     {
         return PlayerPrefs.GetInt("Record_int");
     }
 
     private void SetRecord(int value)
     {
         PlayerPrefs.SetInt("Record_int", value);
     }
 }

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

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

How to switch Audiosource without stacking by an input? 0 Answers

Audio not playing when OnTriggerEnter2D 2 Answers

Problems combining Audio triggers, PlayOneShot and Audio Arrays 1 Answer

2nd Audio Clip Not Playing 0 Answers

Play Audio on Object click? 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