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 alexkarak · Dec 17, 2019 at 05:12 AM · instantiateaudio

audio source on instantiating game object

Hey guys so I have this code and it is added in a game object i am instantiating But when the object is instantiating i get the error "cannot play a disabled audiosource" How can i make it work without changing the audiosource from where it is now?

 using UnityEngine;    using System.Collections;
 
 
  namespace DigitalRuby.PyroParticles   {
      /// <summary>
      /// Provides an easy wrapper to looping audio sources with nice transitions for volume when starting and stopping
      /// </summary>
      public class LoopingAudioSource
      {
          public AudioSource AudioSource { get; private set; }
          public float TargetVolume { get; private set; }
  
          private float startMultiplier;
          private float stopMultiplier;
          private float currentMultiplier;
  
          public LoopingAudioSource(MonoBehaviour script, AudioSource audioSource, float startMultiplier, float stopMultiplier)
          {
              AudioSource = audioSource;
              if (audioSource != null)
              {
                  AudioSource.loop = true;
                  AudioSource.volume = 0.0f;
                  AudioSource.Stop();
              }
  
              TargetVolume = 1.0f;
  
              this.startMultiplier = currentMultiplier = startMultiplier;
              this.stopMultiplier = stopMultiplier;
          }
  
          public void Play()
          {
              Play(TargetVolume);
          }
  
          public void Play(float targetVolume)
          {
              if (AudioSource != null && !AudioSource.isPlaying)
              {
                  AudioSource.volume = 0.0f;
                  AudioSource.Play();
                  currentMultiplier = startMultiplier;
              }
              TargetVolume = targetVolume;
          }
  
          public void Stop()
          {
              if (AudioSource != null && AudioSource.isPlaying)
              {
                  TargetVolume = 0.0f;
                  currentMultiplier = stopMultiplier;
              }
          }
  
          public void Update()
          {
              if (AudioSource != null && AudioSource.isPlaying &&
                  (AudioSource.volume = Mathf.Lerp(AudioSource.volume, TargetVolume, Time.deltaTime / currentMultiplier)) == 0.0f)
              {
                  AudioSource.Stop();
              }
          }
      }
  
      /// <summary>
      /// Script for objects such as wall of fire that never expire unless manually stopped
      /// </summary>
      public class FireConstantBaseScript : FireBaseScript
      {
          [HideInInspector]
          public LoopingAudioSource LoopingAudioSource;
  
          protected override void Awake()
          {
              base.Awake();
  
              // constant effect, so set the duration really high and add an infinite looping sound
              LoopingAudioSource = new LoopingAudioSource(this, AudioSource, StartTime, StopTime);
              Duration = 999999999;
          }
  
          protected override void Update()
          {
              base.Update();
  
              LoopingAudioSource.Update();
          }
  
          protected override void Start()
          {
              base.Start();
  
              LoopingAudioSource.Play();
          }
  
          public override void Stop()
          {
              LoopingAudioSource.Stop();
  
              base.Stop();
          }
      }  }




Comment
Add comment · Show 1
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 aardappel156 · Dec 17, 2019 at 09:16 AM 0
Share

just making sure but is your audiosource disabled in your prefab?

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

176 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

Related Questions

Play An Audio Clip When An Instantiated Object Collides 1 Answer

one clip at different AudioSource 0 Answers

how to sync audio in instantiated object so all players can hear it? 0 Answers

Fading Audio in/out with object creation 1 Answer

Audio and Particles on Trigger 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