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 fleischverpackung · May 08, 2019 at 12:10 PM · instantiateprefabarrayaudiosourceaudioclip

Instanced Prefabs: Audio clip array out of range

Hi guys,

I have prefab characters, each with an audiosource and a tiny script that should play random audioclips from an array. But I always get a "out of range error".

It has to do something with the instantiated prefabs. When i put my script on an object within the scene it works as expected.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Person : MonoBehaviour
 {
 
     private Animator ani;
     private AudioClip[] yeahs;
     public AudioClip[] yeahs2;
     private AudioSource speaker;
 
 
     void Start()
     {
 
        ani = GetComponent<Animator>();
        ani.SetInteger("level", Random.Range(0, 7));
        speaker = GetComponent<AudioSource>();
        yeahs = Resources.LoadAll<AudioClip>("Assets/_Audio/yeah/");
        Debug.Log("YEAHYEAHYEAH" + yeahs);
        
 
     }
 
     void Update()
     {
         new WaitForSeconds(Random.Range(3, 10));
         ani.SetInteger("level", Random.Range(0, 8));
         speaker.PlayOneShot(yeahs2[Random.Range(0, yeahs2.Length -1)]);
         
     }
 }
 

any tipps appreciated!

cheers

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 Legend_Bacon · May 08, 2019 at 12:52 PM

Hello there,


As far as I can tell from this script, you are never populating "yeahs2", which means it effectively has a length of 0.


Of course, as soon as you try to call PlayOneShot() in your Update(), there is nothing to play. So it gives you a null exception.


Since your yeahs2 variable is public, I am guessing you are assigning it somehow in the inspector (or on the prefab itself). Could you share your instantiation code here? There may be something wrong in there...


Other than that, please make sure you understand how the Update() function works in Unity. Your "new WaitForSeconds[...]" line does nothing, for example. Also, I am assuming you don't want to set the Integer of your animator to a random number 60 times per second (the rate at which Update is called), or Play your sound that often.



Hope that helps!

Cheers,

~LegendBacon

Comment
Add comment · Show 2 · 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 fleischverpackung · May 08, 2019 at 01:05 PM 0
Share

Hi!

Yes, the yeas2[] is size 3 and I setup the audioclips within the inspector. Why do you think the WaitforSeconds() does not work? I am triggering the animation controller of my instanciated prefabs with it. As far as I can tell it works. I know that it blocks the update loop, but I don't need a Coroutine in that $$anonymous$$i script.

As mentioned above, if I put the script on an empty gameobject within the scene my code works.

avatar image Legend_Bacon fleischverpackung · May 09, 2019 at 10:42 AM 0
Share

Hello there,


The WaitForSeconds() is not intended to work within "regular" functions. As far as I know, it only works when yielded ( yield return new WaitForSeconds(x);) from within a coroutine (IEnumerator). It does NOT block the update loop.

Try putting a Debug.Log() in your update before and after the line, and you will see that they both trigger in the same frame (and up to 60 times per second as well).

Additionally, your line does not actually wait for anything here. You are just creating a new instance of the WaitForSeconds class. But since you are not assigning it or using it, it is essentially doing nothing.


Could you also tell us exactly which line the error points to? If it is speaker.PlayOneShot(yeahs2[Random.Range(0, yeahs2.Length -1)]);, then try outputting the length of the yeahs2 array. You will see that on at least one of your objects, this will return an invalid length (0 or 1).


Another thing: When used with Integers, Random.Range()'s $$anonymous$$AX value is actually EXCLUSIV$$anonymous$$ Which means you should have Random.Range(0, yeahs2.Length) ins$$anonymous$$d of Random.Range(0, yeahs2.Length -1). Otherwise the last entry will never be selected by the Random function.


And a last point: You script, when thrown on a gameobject in the scene WILL work as is, but not efficiently, and not necessarily as intended (it won't wait, for example). It will call ani.SetInteger("level", Random.Range(0, 8)); and speaker.PlayOneShot(yeahs2[Random.Range(0, yeahs2.Length -1)]); 60 times a second, which is most likely something you don't want.


I hope that helps!

Cheers,

~LegendBacon

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

161 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

Related Questions

Do unused audio sources hurt performance? 0 Answers

Prefabs instantiated from an array are keeping their public int value 1 Answer

Play sound from an array, not random, and only once 3 Answers

Parenting an instantiated prefab. 1 Answer

Passing an AudioClip parameter to Method 1 Answer


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