Skip to main content
added 752 characters in body
Source Link

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, the timer just doesn't count down at all. Please help Please assist in any manner

   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");
    }

    // Use this for initialization
    public void StartTimerStart()
    {
        //StartCoroutine("LoseTime");
        BeginTimer();

    }
    
    // Update is called once per frame
    void Update()
    {
        countdownText.text = ("Time Left = " + timeLeft);

        if (timeLeft <= 0)
        {
            //StopCoroutine("LoseTime");
            //countdownText.text = "Times Up!";
            Invoke("ChangeLevel", 0.1f);
        }
       }
    public void BeginTimer()
    {
        
        Character.GetComponent<PlayerController>().Update();
        if (buttonPressed == true )
        {
            StartCoroutine("LoseTime");
        }
    }

IEnumerator LoseTime()
    {
        while (true)
        {
            yield return new WaitForSeconds(1);
            timeLeft--;
        }

    }
    void ChangeLevel()
    {

        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }
}
    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<Renderer>GetComponent<>().material;;

    public float Speed;
    public Vector2 height;
    public float xMin, xMax, yMin, yMax;
    public bool buttonPressed = false;
    GameObject Character;

    void Awake()
    {
        //playerCharacter = GameObject.FindWithTag("Player");
    }
    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
                {
                    Timer tmrbuttonPressed = GameObject.Find("Timer").GetComponent<Timer>();true;
                    tmr.StartTimer();
                    Character.GetComponent<Rigidbody2D>().AddForce(height, ForceMode2D.Impulse);
             
    }
                Character.GetComponent<Rigidbody2D>().position = new Vector3
                    (
                        Mathf.Clamp(GetComponent<Rigidbody2D>().position.x, xMin, xMax),
                        Mathf.Clamp(GetComponent<Rigidbody2D>().position.y, yMin, yMax)
                    );
                    Timer tmr = GameObject.Find("Timer").GetComponent<Timer>();
                        tmr.BeginTimer();
                        
                    
                }
            }
            catch (System.Exception)
            {


            }
        }
        void ApplicationQuit()
        {
            if (sp != null)
            {

                {
                    sp.Close();
                }
            }
        }
    }
}

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. Right now, the timer just doesn't count down at all. Please help Please assist in any manner

 using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Timer : MonoBehaviour
{
    int startTime = 0;
    public int timeLeft;
    public Text countdownText;

    // Use this for initialization
    public void StartTimer()
    {
        StartCoroutine("LoseTime");
        
    }

    // Update is called once per frame
    void Update()
    {
        countdownText.text = ("Time Left = " + timeLeft);

        if (timeLeft <= 0)
        {
            //StopCoroutine("LoseTime");
            //countdownText.text = "Times Up!";
            Invoke("ChangeLevel", 0.1f);
        }
       }

    IEnumerator LoseTime()
    {
        while (true)
        {
            yield return new WaitForSeconds(1);
            timeLeft--;
        }

    }
    void ChangeLevel()
    {

        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }
}
    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<Renderer>().material;

    public float Speed;
    public Vector2 height;
    public float xMin, xMax, yMin, yMax;

    void Awake()
    {
        //player = GameObject.FindWithTag("Player");
    }
    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
                {
                    Timer tmr = GameObject.Find("Timer").GetComponent<Timer>();
                    tmr.StartTimer();
                    GetComponent<Rigidbody2D>().AddForce(height, ForceMode2D.Impulse);
                }
                GetComponent<Rigidbody2D>().position = new Vector3
                (
                    Mathf.Clamp(GetComponent<Rigidbody2D>().position.x, xMin, xMax),
                    Mathf.Clamp(GetComponent<Rigidbody2D>().position.y, yMin, yMax)
                );
            }
            catch (System.Exception)
            {


            }
        }
        void ApplicationQuit()
        {
            if (sp != null)
            {

                {
                    sp.Close();
                }
            }
        }
    }
}

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, the timer just doesn't count down at all. Please help Please assist in any manner

   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");
    }

    // Use this for initialization
    public void Start()
    {
        //StartCoroutine("LoseTime");
        BeginTimer();

    }
    
    // Update is called once per frame
    void Update()
    {
        countdownText.text = ("Time Left = " + timeLeft);

        if (timeLeft <= 0)
        {
            //StopCoroutine("LoseTime");
            //countdownText.text = "Times Up!";
            Invoke("ChangeLevel", 0.1f);
        }
       }
    public void BeginTimer()
    {
        
        Character.GetComponent<PlayerController>().Update();
        if (buttonPressed == true )
        {
            StartCoroutine("LoseTime");
        }
    }

IEnumerator LoseTime()
    {
        while (true)
        {
            yield return new WaitForSeconds(1);
            timeLeft--;
        }

    }
    void ChangeLevel()
    {

        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }
}
    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<>();

    public float Speed;
    public Vector2 height;
    public float xMin, xMax, yMin, yMax;
    public bool buttonPressed = false;
    GameObject Character;

    void Awake()
    {
        Character = GameObject.FindWithTag("Player");
    }
    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<Rigidbody2D>().AddForce(height, ForceMode2D.Impulse);
 
                    Character.GetComponent<Rigidbody2D>().position = new Vector3
                    (
                        Mathf.Clamp(GetComponent<Rigidbody2D>().position.x, xMin, xMax),
                        Mathf.Clamp(GetComponent<Rigidbody2D>().position.y, yMin, yMax)
                    );
                    Timer tmr = GameObject.Find("Timer").GetComponent<Timer>();
                        tmr.BeginTimer();
                        
                    
                }
            }
            catch (System.Exception)
            {


            }
        }
        void ApplicationQuit()
        {
            if (sp != null)
            {

                {
                    sp.Close();
                }
            }
        }
    }
}
Tried to reference timer script in player controller script
Source Link

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. Right now, the timer just doesn't count down at all. Please help Please assist in any manner

The Arduino code:

#include<SoftwareSerial.h>
#include <Wire.h>

const int ledPin = 13; // choose the pin for the LED
const int button1 = 2; // choose the input pin (for 1st pushbutton)
const int button2 = 3; // choose the input pin (for 2nd pushbutton)


  int ledState = LOW;         // the current state of the output pin
  int buttonState;             // the current reading from the input pin
  int lastButtonState = HIGH;   // the previous reading from the input pin
  unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
  unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
void setup() 
{
  Serial.begin(9600); // Start the serial transmission
  digitalWrite(ledPin, ledState);
  pinMode(ledPin, OUTPUT); // declare LED as output
  pinMode(button1, INPUT); // declare pushbutton as input
  pinMode(button2, INPUT); // declare pushbutton as input
 
}
  
void loop()
{
  int valA = digitalRead(button1); // read button stateint 
 int  valB = digitalRead(button2); // read button state
   if (valA == LOW &&valB == LOW ) // check if the input is HIGH for both buttons -this can be changed to ||(or) or !(not)
  {
    digitalWrite(ledPin, HIGH); // turn LED on if both switches are pressed
    Serial.println("0");
      delay(20);
  }
    else
    {
      digitalWrite(ledPin, LOW); // turnLED off}}
      Serial.println("1");
    }
 int reading = digitalRead(INPUT);
 if (reading != lastButtonState) {
    
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    
    if (reading != buttonState) {
      buttonState = reading;

      
      if (buttonState == LOW) {
        ledState = !ledState;
      }
    }
  }


  digitalWrite(ledPin, ledState);

  
  lastButtonState = reading;
}
 
 using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Timer : MonoBehaviour
{
    Image timerBar;
    //public int maxTimestartTime = 5;0;
    public int timeLeft;
    public Text countdownText;

    // Use this for initialization
    public void StartStartTimer()
    {
        StartCoroutine("LoseTime");
        timerBar = GetComponent<Image>();
    }

    // Update is called once per frame
    void Update()
    {
        countdownText.text = ("Time Left = " + timeLeft);

        if (timeLeft <= 0)
        {
            //StopCoroutine("LoseTime");
            //countdownText.text = "Times Up!";
            Invoke("ChangeLevel", 0.1f);
        }
       /* else if (timeLeft > 0)
        {
            timerBar.fillAmount = timeLeft / maxTime;
        }*/

    }

    IEnumerator LoseTime()
    {
        while (true)
        {
            yield return new WaitForSeconds(1);
            timeLeft--;
        }

    }
    void ChangeLevel()
    {

        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }
}
    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<Renderer>().material;

    public float Speed;
    public Vector2 height;
    public float xMin, xMax, yMin, yMax;

    void Awake()
    {
        //player = GameObject.FindWithTag("Player");
    }
    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
                {
                    Timer tmr = GameObject.Find("Timer").GetComponent<Timer>();
                    tmr.StartTimer();
                    GetComponent<Rigidbody2D>().AddForce(height, ForceMode2D.Impulse);
                }
                GetComponent<Rigidbody2D>().position = new Vector3
                (
                    Mathf.Clamp(GetComponent<Rigidbody2D>().position.x, xMin, xMax),
                    Mathf.Clamp(GetComponent<Rigidbody2D>().position.y, yMin, yMax)
                );
            }
            catch (System.Exception)
            {


            }
        }
        void ApplicationQuit()
        {
            if (sp != null)
            {

                {
                    sp.Close();
                }
            }
        }
    }
}

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.

  Please assist in any manner

The Arduino code:

#include<SoftwareSerial.h>
#include <Wire.h>

const int ledPin = 13; // choose the pin for the LED
const int button1 = 2; // choose the input pin (for 1st pushbutton)
const int button2 = 3; // choose the input pin (for 2nd pushbutton)


  int ledState = LOW;         // the current state of the output pin
  int buttonState;             // the current reading from the input pin
  int lastButtonState = HIGH;   // the previous reading from the input pin
  unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
  unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
void setup() 
{
  Serial.begin(9600); // Start the serial transmission
  digitalWrite(ledPin, ledState);
  pinMode(ledPin, OUTPUT); // declare LED as output
  pinMode(button1, INPUT); // declare pushbutton as input
  pinMode(button2, INPUT); // declare pushbutton as input
 
}
  
void loop()
{
  int valA = digitalRead(button1); // read button stateint 
 int  valB = digitalRead(button2); // read button state
   if (valA == LOW &&valB == LOW ) // check if the input is HIGH for both buttons -this can be changed to ||(or) or !(not)
  {
    digitalWrite(ledPin, HIGH); // turn LED on if both switches are pressed
    Serial.println("0");
      delay(20);
  }
    else
    {
      digitalWrite(ledPin, LOW); // turnLED off}}
      Serial.println("1");
    }
 int reading = digitalRead(INPUT);
 if (reading != lastButtonState) {
    
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    
    if (reading != buttonState) {
      buttonState = reading;

      
      if (buttonState == LOW) {
        ledState = !ledState;
      }
    }
  }


  digitalWrite(ledPin, ledState);

  
  lastButtonState = reading;
}
 
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Timer : MonoBehaviour
{
    Image timerBar;
    //public int maxTime = 5;
    public int timeLeft;
    public Text countdownText;

    // Use this for initialization
    void Start()
    {
        StartCoroutine("LoseTime");
        timerBar = GetComponent<Image>();
    }

    // Update is called once per frame
    void Update()
    {
        countdownText.text = ("Time Left = " + timeLeft);

        if (timeLeft <= 0)
        {
            //StopCoroutine("LoseTime");
            //countdownText.text = "Times Up!";
            Invoke("ChangeLevel", 0.1f);
        }
       /* else if (timeLeft > 0)
        {
            timerBar.fillAmount = timeLeft / maxTime;
        }*/

    }

    IEnumerator LoseTime()
    {
        while (true)
        {
            yield return new WaitForSeconds(1);
            timeLeft--;
        }

    }
    void ChangeLevel()
    {

        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }
}
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<Renderer>().material;

    public float Speed;
    public Vector2 height;
    public float xMin, xMax, yMin, yMax;

    void Awake()
    {
        //player = GameObject.FindWithTag("Player");
    }
    void Start()
    {
        if (!sp.IsOpen)
        { // If the erial port is not open 
            sp.Open(); // Open 
        }
        sp.ReadTimeout = 1; // Timeout for reading
    }

    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
                {
                    GetComponent<Rigidbody2D>().AddForce(height, ForceMode2D.Impulse);
                }
                GetComponent<Rigidbody2D>().position = new Vector3
                (
                    Mathf.Clamp(GetComponent<Rigidbody2D>().position.x, xMin, xMax),
                    Mathf.Clamp(GetComponent<Rigidbody2D>().position.y, yMin, yMax)
                );
            }
            catch (System.Exception)
            {


            }
        }
        void ApplicationQuit()
        {
            if (sp != null)
            {

                {
                    sp.Close();
                }
            }
        }
    }
}

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. Right now, the timer just doesn't count down at all. Please help Please assist in any manner

 using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Timer : MonoBehaviour
{
    int startTime = 0;
    public int timeLeft;
    public Text countdownText;

    // Use this for initialization
    public void StartTimer()
    {
        StartCoroutine("LoseTime");
        
    }

    // Update is called once per frame
    void Update()
    {
        countdownText.text = ("Time Left = " + timeLeft);

        if (timeLeft <= 0)
        {
            //StopCoroutine("LoseTime");
            //countdownText.text = "Times Up!";
            Invoke("ChangeLevel", 0.1f);
        }
       }

    IEnumerator LoseTime()
    {
        while (true)
        {
            yield return new WaitForSeconds(1);
            timeLeft--;
        }

    }
    void ChangeLevel()
    {

        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }
}
    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<Renderer>().material;

    public float Speed;
    public Vector2 height;
    public float xMin, xMax, yMin, yMax;

    void Awake()
    {
        //player = GameObject.FindWithTag("Player");
    }
    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
                {
                    Timer tmr = GameObject.Find("Timer").GetComponent<Timer>();
                    tmr.StartTimer();
                    GetComponent<Rigidbody2D>().AddForce(height, ForceMode2D.Impulse);
                }
                GetComponent<Rigidbody2D>().position = new Vector3
                (
                    Mathf.Clamp(GetComponent<Rigidbody2D>().position.x, xMin, xMax),
                    Mathf.Clamp(GetComponent<Rigidbody2D>().position.y, yMin, yMax)
                );
            }
            catch (System.Exception)
            {


            }
        }
        void ApplicationQuit()
        {
            if (sp != null)
            {

                {
                    sp.Close();
                }
            }
        }
    }
}
Source Link

Using a timer in conjunction with two push buttons from Arduino input

noob here

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.

Please assist in any manner

The Arduino code:

#include<SoftwareSerial.h>
#include <Wire.h>

const int ledPin = 13; // choose the pin for the LED
const int button1 = 2; // choose the input pin (for 1st pushbutton)
const int button2 = 3; // choose the input pin (for 2nd pushbutton)


  int ledState = LOW;         // the current state of the output pin
  int buttonState;             // the current reading from the input pin
  int lastButtonState = HIGH;   // the previous reading from the input pin
  unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
  unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
void setup() 
{
  Serial.begin(9600); // Start the serial transmission
  digitalWrite(ledPin, ledState);
  pinMode(ledPin, OUTPUT); // declare LED as output
  pinMode(button1, INPUT); // declare pushbutton as input
  pinMode(button2, INPUT); // declare pushbutton as input
 
}
  
void loop()
{
  int valA = digitalRead(button1); // read button stateint 
 int  valB = digitalRead(button2); // read button state
   if (valA == LOW &&valB == LOW ) // check if the input is HIGH for both buttons -this can be changed to ||(or) or !(not)
  {
    digitalWrite(ledPin, HIGH); // turn LED on if both switches are pressed
    Serial.println("0");
      delay(20);
  }
    else
    {
      digitalWrite(ledPin, LOW); // turnLED off}}
      Serial.println("1");
    }
 int reading = digitalRead(INPUT);
 if (reading != lastButtonState) {
    
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    
    if (reading != buttonState) {
      buttonState = reading;

      
      if (buttonState == LOW) {
        ledState = !ledState;
      }
    }
  }


  digitalWrite(ledPin, ledState);

  
  lastButtonState = reading;
}
 

The Timer script:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Timer : MonoBehaviour
{
    Image timerBar;
    //public int maxTime = 5;
    public int timeLeft;
    public Text countdownText;

    // Use this for initialization
    void Start()
    {
        StartCoroutine("LoseTime");
        timerBar = GetComponent<Image>();
    }

    // Update is called once per frame
    void Update()
    {
        countdownText.text = ("Time Left = " + timeLeft);

        if (timeLeft <= 0)
        {
            //StopCoroutine("LoseTime");
            //countdownText.text = "Times Up!";
            Invoke("ChangeLevel", 0.1f);
        }
       /* else if (timeLeft > 0)
        {
            timerBar.fillAmount = timeLeft / maxTime;
        }*/

    }

    IEnumerator LoseTime()
    {
        while (true)
        {
            yield return new WaitForSeconds(1);
            timeLeft--;
        }

    }
    void ChangeLevel()
    {

        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }
}

The Player controller script:

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<Renderer>().material;

    public float Speed;
    public Vector2 height;
    public float xMin, xMax, yMin, yMax;

    void Awake()
    {
        //player = GameObject.FindWithTag("Player");
    }
    void Start()
    {
        if (!sp.IsOpen)
        { // If the erial port is not open 
            sp.Open(); // Open 
        }
        sp.ReadTimeout = 1; // Timeout for reading
    }

    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
                {
                    GetComponent<Rigidbody2D>().AddForce(height, ForceMode2D.Impulse);
                }
                GetComponent<Rigidbody2D>().position = new Vector3
                (
                    Mathf.Clamp(GetComponent<Rigidbody2D>().position.x, xMin, xMax),
                    Mathf.Clamp(GetComponent<Rigidbody2D>().position.y, yMin, yMax)
                );
            }
            catch (System.Exception)
            {


            }
        }
        void ApplicationQuit()
        {
            if (sp != null)
            {

                {
                    sp.Close();
                }
            }
        }
    }
}

Any tips and assistance will be appreciated