• 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 SuperSparkplug · Apr 07, 2013 at 01:39 PM · buttonframeosc

Make a function last multiple frames

Hi there.

I'm currently working on my Sparky Konga application (Example Video) and I'm trying to fix an issue I'm having with the microphone going off accidentally when I hit the bongos. Since then, I'm currently, I'm telling the microphone to go to a value of 0 everytime it gets an OSC Message from the microphone that it hears something. However' I realize that because it's all within the update function, it will only activate for a single frame that the button is pressed. To avoid it hearing me hit the bongos and the vibration as often, I want to see if I can get the function telling the mic to go to zero last multiple frames to minimize the chances of it messing up. However, I'm not sure at all how exactly to make something like that last multiple frames.

Here's my source code. What would I need to change to accomplish this?

 using UnityEngine;
 using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Text;
 using UnityOSC;
 
 public class BoncoClapScript : MonoBehaviour {
     
     public int burst = 500;
     public AudioClip clap;
     public AudioClip clap2;
     public bool alt;
     public int filter = 14;
     private Dictionary<string, ServerLog> servers;
     
     /*public Light flash;
     private float lerpTime = 0.5f;
     private float i = 0;*/
     
     // Use this for initialization
     void Start () {
         OSCHandler.Instance.Init();
         servers = new Dictionary<string, ServerLog>(); 
         particleSystem.emissionRate = 0;
     }
     
     /*public void OSCMessageReceived(OSC.NET.OSCMessage message){
     string address = message.Address;
     ArrayList args = message.Values;
     }*/
     
     // Update is called once per frame
     void Update () {
         
         if (Input.GetKey(KeyCode.JoystickButton9) || Input.GetKey(KeyCode.S))    {
             alt = !alt;            
         }
         
         if (Input.GetKey(KeyCode.JoystickButton9) || Input.GetKey(KeyCode.S) && alt == true)    {
             //alt = alt;
         }
         
         OSCHandler.Instance.UpdateLogs();
         servers = OSCHandler.Instance.Servers;
         
         foreach( KeyValuePair<string, ServerLog> item in servers )
         {
             // If we have received at least one packet,
             // show the last received from the log in the Debug console
             if(item.Value.log.Count > 0) 
             {
                 int lastPacketIndex = item.Value.packets.Count - 1;
             
                 Debug.Log(String.Format("SERVER: {0} ADDRESS: {1} VALUE 0: {2}", 
                                                     item.Key, // Server name
                                                     item.Value.packets[lastPacketIndex].Address, // OSC address
                                                     item.Value.packets[lastPacketIndex].Data[0].ToString())); //First data value
         
                 int mic = (int) item.Value.packets[lastPacketIndex].Data[0];
                 Debug.Log("microphone" + mic);
             
                 //Triggers Particles, sounds, and sets buffer for mic values
                 if (mic >= filter || (Input.GetKeyDown(KeyCode.W))  && alt == false) {
                     particleSystem.Emit(burst);
                     audio.PlayOneShot(clap);
                     //flash.range = (5 - Time.deltaTime);
                 
                 if (mic >= filter && alt == true) {
                     particleSystem.Emit(burst);
                     audio.PlayOneShot(clap2);
                     //flash.range = (5 - Time.deltaTime);
                     }
                     
                 if (Input.GetKeyDown(KeyCode.JoystickButton0) || Input.GetKeyDown(KeyCode.JoystickButton1) || Input.GetKeyDown(KeyCode.JoystickButton2) || Input.GetKeyDown(KeyCode.JoystickButton3)) {
                         mic = 0;
                     }
                 
             }
             
         }
         
         }
         
         if ((Input.GetKeyDown(KeyCode.W))) {
                     particleSystem.Emit(burst);
                     audio.PlayOneShot(clap);
                     }
                 
                 if (Input.GetKeyDown(KeyCode.W) && alt == true) {
                     particleSystem.Emit(burst);
                     audio.PlayOneShot(clap2);
                     }
     
     }
 }
 
 


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 Fattie · Apr 07, 2013 at 08:46 PM 0
Share

quite simply nobody will read that much code.

it's possible you need to learn about yield .. if you're not familiar with it yet, go for it.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

Controlling multiple booleans 1 Answer

How can I stop getting mouse position of Ui Elements? 0 Answers

How to put an Image inside middle of text? 0 Answers

UI elements are not getting affected by lighting 0 Answers

Creating a level switch by using mouse function in a certain area 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