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 /
  • Help Room /
avatar image
0
Question by Aether_Entity34 · Aug 04, 2016 at 07:14 PM · scripting problemarrayaudio

I Need help with a music cycle generator in C#

I am trying to have a script that controls six songs in a daylight cycle- 3 for night & 3 for day controlled by arrays. I have researched for many hours and have found nothing.

Here is the script


using UnityEngine; using System.Collections;

public class MusicGenerator : MonoBehaviour {

 void Start ()
 {
     dayMusic = new AudioClip[3];

     playDayMusic();
 }
 public AudioClip[] dayMusic;
 public bool isDay = true;
 public AudioClip currentMusic;

 // Update is called once per frame
 void Update ()
 {
     if(isDay == true)
     {
         
     }
 }

 void playDayMusic()
 {
     AudioSource audio = GetComponent<AudioSource>();
     audio.clip = dayMusic[Random.Range(0, dayMusic.Length)];
     audio.Play();

 }

}

Help would be great.

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 Joshulloydwilliams · Apr 29, 2021 at 10:13 AM

I'm kind of new to unity so this may no be the best way, but i had a similar issue, i wanted to cycle multiple background songs randomly one after the other, it should be easy to modify it to factor in day / night. To do this i had three scripts, one that generated the songs randomly, one that told the generation script when the song was over, and one that was used for publicly accessible variables, i also attached each song i wanted to use onto their own empty prefab in the assets menu. in this example i had three songs.

 // this is the variable script, it is attached to an empty game object so that it's a prefab.
 using UnityEngine;
 public class Variable : MonoBehaviour
 {
    public bool MusicOn;
 }

 //this script uses a random number to randomly generate the songs, it is attached to an empty   //game object in scene
 using UnityEngine;
 public class musicGenerator : MonoBehaviour
 {
 // for each song you need to create a GameObject.
     public GameObject music1;
     public GameObject music2;
     public GameObject music3;
     private int whichMusic;
 // this part of the script is needed for the publicly accessible variables to work
     private Variable variables;
 //this is what you attach the variable prefab to
     public GameObject Variables;
     void Awake()
     {
         variables = Variables.GetComponent<Variable>();
     }
     void Start (){
         variables.MusicOn = false;
     }
     void Update (){
         whichMusic = Random.Range(1,4);
             if(variables.MusicOn == false){
                 if(whichMusic == 1){
                     Instantiate(music1);
 // this prevents an infinite loop
                     variables.MusicOn = true;
                 }
                 if(whichMusic == 2){
                     Instantiate(music3);
                     variables.MusicOn = true;
                 }
                 if(whichMusic == 3){
                     Instantiate(music3);
                     variables.MusicOn = true;
                 }
             }
         }    
     }
 }

 // this script uses the date tell when the song started, when it should stop and the current   //time, this script needs to be attached to each music prefab e.g. music 1.
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 public class MusicControl1 : MonoBehaviour
 {
  // musicDuration is manually put in and is the duration of the song in seconds
 public float musicDuration;
 //this is what you attach the variable prefab
 private Variable variables;
 public GameObject Variables;
 System.DateTime musicTimeToReach;
 void Awake()
 {
     variables = Variables.GetComponent<Variable>();
 }
 void Start(){
      musicTimeToReach = System.DateTime.Now;
      musicTimeToReach = musicTimeToReach.AddSeconds(musicDuration);
         Debug.Log(musicTimeToReach);
 }
 void Update()
 {
    System.DateTime myTime = System.DateTime.Now;
    Debug.Log(myTime);
     if(musicTimeToReach <= myTime){
         variables.MusicOn = false;
         Destroy(gameObject);
     }
     if(variables.gameOn == false){
         variables.MusicOn = false;
         Destroy(gameObject);}
     }
 }
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

89 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

Related Questions

Soundarray for player is not working properly 1 Answer

Question on Audio? 1 Answer

Wher is the problem? My string isn't behaving... 1 Answer

change Volume of Audiomixergroups by script 1 Answer

Help with script for Main Menu? 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