Question by
ProjetoAIB121C · May 19 at 11:03 AM ·
scripting problemunity 2dvariableslogic
Code wont work, even though the logic seems right to me
I am trying to make a wack-a-mole for two players. They have a 60 seconds to score as much points as possible.
The two scripts are the main an a timer, as follows. I am also trying to make take the timeVallue
variable from the second scrip to the first.
Can anyone help?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Jogo : MonoBehaviour
{
[SerializeField] Text gameText;
[SerializeField] SpriteRenderer Square1, Square2, Square3, Square4;
float p1points, p2points;
int randomDelay, randomNumber;
void Start()
{
p1points = 0f;
p2points = 0f;
randomDelay = 0;
randomNumber = 0;
Square1.color = new Color(255f, 0f, 0f, 1f);
Square2.color = new Color(255f, 0f, 0f, 1f);
Square3.color = new Color(255f, 0f, 0f, 1f);
Square4.color = new Color(255f, 0f, 0f, 1f);
}
void Update()
{
if (timeVallue <= 0)
{
Debug.Log("Jogo acabou");
if(p1points > p2points)
{
gameText.text = "Jogador 1 Ganhou!!!";
Debug.Log("Jogador 1 ganhou");
}
else if(p1points < p2points)
{
gameText.text = "Jogador 2 Ganhou!!!";
Debug.Log("Jogador 2 ganhou");
}
else
{
gameText.text = "Empate!!!";
Debug.Log("Empate");
}
System.Threading.Thread.Sleep(3000);
Application.Quit();
}
while(timeVallue > 0)
{
randomDelay = Random.Range(500, 3000);
System.Threading.Thread.Sleep(randomDelay);
Debug.Log("Espera");
randomNumber = Random.Range(1, 4);
if(randomNumber == 1)
{
Square1.color = new Color(0f, 255f, 0f, 1);
if (Input.GetKey(KeyCode.W))
{
p1points++;
Square1.color = new Color(255f, 0f, 0f, 1f);
Debug.Log("Ponto p1");
}
if (Input.GetKey(KeyCode.UpArrow))
{
p2points++;
Square1.color = new Color(255f, 0f, 0f, 1f);
Debug.Log("Ponto p2");
}
}
else if (randomNumber == 2)
{
Square2.color = new Color(0f, 255f, 0f, 1);
if (Input.GetKey(KeyCode.A))
{
p1points++;
Square2.color = new Color(255f, 0f, 0f, 1f);
Debug.Log("Ponto p1");
}
if (Input.GetKey(KeyCode.LeftArrow))
{
p2points++;
Square2.color = new Color(255f, 0f, 0f, 1f);
Debug.Log("Ponto p2");
}
}
else if (randomNumber == 3)
{
Square3.color = new Color(0f, 255f, 0f, 1);
if (Input.GetKey(KeyCode.D))
{
p1points++;
Square3.color = new Color(255f, 0f, 0f, 1f);
Debug.Log("Ponto p1");
}
if (Input.GetKey(KeyCode.RightArrow))
{
p2points++;
Square3.color = new Color(255f, 0f, 0f, 1f);
Debug.Log("Ponto p2");
}
}
else if (randomNumber == 4)
{
Square4.color = new Color(0f, 255f, 0f, 1f);
if (Input.GetKey(KeyCode.S))
{
p1points++;
Square4.color = new Color(255f, 0f, 0f, 1f);
Debug.Log("Ponto p2");
}
if (Input.GetKey(KeyCode.DownArrow))
{
p2points++;
Square4.color = new Color(255f, 0f, 0f, 1f);
Debug.Log("Ponto p2");
}
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Timer : MonoBehaviour
{
public float timeVallue = 60;
public Text timeText;
void Update()
{
if(timeVallue>0)
{
timeVallue -= Time.deltaTime;
}
DisplayTime(timeVallue);
}
void DisplayTime(float timeToDisplay)
{
if(timeToDisplay < 0)
{
timeToDisplay = 0;
}
else if(timeToDisplay > 0)
{
timeToDisplay += 1;
}
float minutes = Mathf.FloorToInt(timeToDisplay / 60);
float seconds = Mathf.FloorToInt(timeToDisplay % 60);
timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
}
}
Comment