• 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 /
This question was closed Jun 07, 2019 at 11:35 AM by shpakrecords for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by shpakrecords · Jun 06, 2019 at 05:26 PM · particlessoundparticle systemsound effectsorder-of-execution

Cant get sound playing BEFORE event happened.

It is the first time I am encountering this issue. I am making a script that changes the effect of wind on a Player after X amount of time and which lasts for Y amount of time. I need my sound to play together with this effect, however, it always plays only AFTER the event finishes. Here is my script:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class SnowScript : MonoBehaviour { public static SnowScript instance;

 AudioManager audioManager;
 IndentScript indentScript;

 Vector2 originalPos = new Vector2(-6.51f,7.53f);
 Quaternion originalRot = Quaternion.Euler(110,-90,-90);
 ParticleSystem ps;

 public float delay;
 [Range(0f, 2f)]
 public float randomDelay;
 public bool swapped;
 public float snowAffect;
 public float timer;
 public float burstDelay;
 [Range(0f, 2f)]
 public float randomBurstDelay;
 public float snowBurstAffect;
 public bool burst;
 public float burstTimer;
 public float burstActiveTime;

 void Awake()
 {
     instance = this;
     timer = delay;
     burstTimer = burstDelay;
     transform.position = originalPos;
     transform.rotation = originalRot;
     ps = GetComponent<ParticleSystem>();
 }
 void Start()
 {
     audioManager = AudioManager.instance;
     indentScript = IndentScript.instance;
 }
 void Update()
 {
     CheckTimers();
     CheckSwap();
 }
 void CheckTimers()
 {
     if (timer >= 0)
     {
         timer -= Time.deltaTime;
     }
     if (timer <= 0)
     {
         swapped = !swapped;
         Swap();
     }
     if (burstTimer > 0)
     {
         burstTimer -= Time.deltaTime;
     }
     if (burstTimer <= 0)
     {
         burst = true;
     }
 }
 void CheckSwap()
 {
     if (swapped)
     {
         if (!burst)
         {
             var main = ps.main;
             main.simulationSpeed = 1f;
             indentScript.transform.Translate(Vector3.left * snowAffect * Time.deltaTime);
         }
         if (burst)
         {
             var main = ps.main;
             main.simulationSpeed = 3f;
             StartCoroutine("Wait");
             indentScript.transform.Translate(Vector3.left * snowBurstAffect * Time.deltaTime);
         }
     }
     if (!swapped)
     {
         if (!burst)
         {
             var main = ps.main;
             main.simulationSpeed = 1f;
             indentScript.transform.Translate(Vector3.right * snowAffect * Time.deltaTime);
         }
         if (burst)
         {
             var main = ps.main;
             main.simulationSpeed = 3f;
             StartCoroutine("Wait");
             indentScript.transform.Translate(Vector3.right * snowBurstAffect * Time.deltaTime);
         }
     }
 }
 void Swap()
 {
     if (!swapped)
     {
         transform.position = originalPos;
         transform.rotation = originalRot;
     }
     if (swapped)
     {
         transform.position = new Vector2(10.91f, 7.53f);
         transform.rotation = Quaternion.Euler(20, -90, -90);
     }
     timer = delay * (1 + Random.Range(-randomDelay / 2f, randomDelay / 2f));
 }
 IEnumerator Wait()
 {
     //audioManager.PlaySound("WindBurst");  //    - Sound should play BEFORE the WaitForSeconds, however it only plays after.
     yield return new WaitForSeconds(burstActiveTime);
     burst = false;
     burstTimer = burstDelay * (1 + Random.Range(-randomBurstDelay / 2f, randomBurstDelay / 2f));
 }

}

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

  • Sort: 
avatar image
0
Best Answer

Answer by shpakrecords · Jun 07, 2019 at 11:34 AM

Figured out by myself. Apparently, sound does not want to play if it is simply in update. Added an extra condition to check:

void PlayAudio() { if (burst && windBurst.isPlaying == false) { windBurst.Play(); } if (!burst && windBurst.isPlaying) { windBurst.Stop(); } }

Maybe this will help somebody!

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

188 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Particle Effect appears low-quality in-game. 0 Answers

Some question about ParticleSystem.emit() rendering 0 Answers

Can I shoot a ray from a particle that hit a collider? 1 Answer

ParticleSystem.Bake() not working with specific particle 0 Answers

How should I implement rolling ball sounds? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges