title: Using a timer in conjunction with 2 push buttons from arduino
body: <p>So I am using two push buttons (connected to an Arduino Uno) as an input to my game. The player has to push down both buttons at the same time for the character to move in the game. I want the player to hold down the buttons for a different amount of time in each level. I have a working Arduino and a working Unity timer and player script, but am not able to get the code to do what I want. What I basically want is that only when the player presses the buttons down, does the timer start counting down. Right now, the timer starts as soon as the scene begins. I know that I somehow have to reference the timer script to the button object, I have tried this but it still doesn't work. Note that the timer UI does have a Timer tag on it. I have also referenced the Player Controller script in the Timer script. Right now, Its giving me a range of errors. I have attached an image depicting these errors.<a href="https://i.stack.imgur.com/jzVml.png" rel="nofollow noreferrer">error image</a>
  The Timer script:</p>
  
  <pre class="lang-c# prettyprint-override"><code>    using UnityEngine;
  using System.Collections;
  using UnityEngine.UI;
  using UnityEngine.SceneManagement;
  
  public class Timer : MonoBehaviour
  {
      //int startTime = 0;
      public bool buttonPressed = false;
      public int timeLeft;
      public Text countdownText;
      GameObject Character;
      void Awake()
      {
          Character = GameObject.FindWithTag("Player");
      }
  
      public void Start()
      {
          //StartCoroutine("LoseTime");
          BeginTimer();
  
      }
  
      void Update()
      {
          countdownText.text = ("Time Left = " + timeLeft);
  
          if (timeLeft &lt;= 0)
          {
              //StopCoroutine("LoseTime");
              //countdownText.text = "Times Up!";
              Invoke("ChangeLevel", 0.1f);
          }
         }
      public void BeginTimer()
      {
          Character.GetComponent&lt;PlayerController&gt;().Update();
  
          //gameObject.GetComponent&lt;MyScript2&gt;().MyFunction();
          if (buttonPressed == true )
          {
              StartCoroutine("LoseTime");
          }
          else if (buttonPressed == false)
          {
              StopCoroutine("LoseTime");
          }
      }
  
  IEnumerator LoseTime()
      {
          while (true)
          {
              yield return new WaitForSeconds(1);
              timeLeft--;
          }
  
      }
      void ChangeLevel()
      {
  
          SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
      }
  }
  </code></pre>
  
  <p>The Player Script:</p>
  
  <pre class="lang-c# prettyprint-override"><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using System.IO.Ports;
  
  public class PlayerController : MonoBehaviour
  {
      SerialPort sp = new SerialPort("\\\\.\\COM4", 9600);
      //player == GameObject.FindWithTag("Player").GetComponent&lt;&gt;();
  
      public float Speed;
      public Vector2 height;
      public float xMin, xMax, yMin, yMax;
      public bool buttonPressed = false;
      GameObject Character;
  
      public void Awake()
      {
          Character = GameObject.FindWithTag("Player");
      }
      public void Start()
      {
          if (!sp.IsOpen)
          { // If the erial port is not open 
              sp.Open(); // Open 
          }
          sp.ReadTimeout = 1; // Timeout for reading
      }
  
      public void Update()
      {
  
          if (sp.IsOpen)
          { // Check to see if the serial port is open 
              try
              {
  
                  string value = sp.ReadLine();//To("Button"); //Read the information
                  int button = int.Parse(value);
                  //float amount = float.Parse(value);
                  //transform.Translate(Speed * Time.deltaTime, 0f, 0f);  //walk
  
  
                  if (button == 0) //*Input.GetKeyDown(KeyCode.Space*/)  //jump
                  {
                      buttonPressed = true;
  
                      Character.GetComponent&lt;Rigidbody2D&gt;().AddForce(height, ForceMode2D.Impulse);
  
                      Character.GetComponent&lt;Rigidbody2D&gt;().position = new Vector3
                      (
                          Mathf.Clamp(GetComponent&lt;Rigidbody2D&gt;().position.x, xMin, xMax),
                          Mathf.Clamp(GetComponent&lt;Rigidbody2D&gt;().position.y, yMin, yMax)
                      );
                      Timer tmr = GameObject.Find("Timer").GetComponent&lt;Timer&gt;();
                          tmr.BeginTimer();
  
  
                  }
              }
              catch (System.Exception)
              {
  
  
              }
          }
          void ApplicationQuit()
          {
              if (sp != null)
              {
  
                  {
                      sp.Close();
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>I think the problem may be with how I am referencing the scripts in each other.</p>