Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 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
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

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.

Follow this Question

Answers Answers and Comments

299 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 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 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 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

Handle order in layer in 2D game ? 2 Answers

Easiest way to check spelling within Unity's STORY TEXT AREA? 0 Answers

Why is Unity showing the wrong variables in the Inspector? 1 Answer

{SOLVED, but not realy} working on a space shooter game and im having problems with my upgrade buttons. 1 Answer

[SOLVED]bool is getting reset by no reason 2 Answers


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