Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by TheNucleaLion · Aug 31, 2020 at 10:33 PM · 2d gametimescene-loadingspeedtetris

Scene doesn't reload properly

Hello!

My project is cloning tetris. I added a MainMenu and a GameOver scene. In the GameOver scene I made a "respawn" button which uses SceneManager to re-load the MainGameScene. But after loading it from the GameOver scene, the speed is the same it was when the MainGameScene ended (very fast) instead of being how it was when firstly loading the MainGameScene (slow). Can anyone help me? I think it's because of increasing speed after a certain amount of time, which doesn't reset when I reload the scene. But since I'm a beginner, I'm not sure how to fix it.. Here is my code:

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.UI;
  using UnityEngine.SceneManagement;
  
  public class Shape : MonoBehaviour
  {
      public static float speed = 1.0f;
  
      float lastMoveDown = 0;
  
      void Start()
      {
          if(!IsInGrid())
          {
              SoundManager.Instance.PlayOneShot(SoundManager.Instance.gameOver);
  
              Invoke("OpenGameOverScene", .5f);
          }
  
          InvokeRepeating("IncreaseSpeed", 2.0f, 2.0f);
      }
  
      void OpenGameOverScene()
      {
          Destroy(gameObject);
          SceneManager.LoadScene("GameOver");
      }
  
      void IncreaseSpeed()
      {
          Shape.speed -= .001f;
      }
  
      // Update is called once per frame
      void Update()
      {
          if(Input.GetKeyDown("a"))
          {
              transform.position += new Vector3(-1, 0, 0);
  
              if(!IsInGrid())
              {
                  transform.position += new Vector3(1, 0, 0);
              }
              else
              {
                  UpdateGameBoard();
                  SoundManager.Instance.PlayOneShot(SoundManager.Instance.shapeMove);
              }
          }
  
          if (Input.GetKeyDown("d"))
          {
              transform.position += new Vector3(1, 0, 0);
  
              if (!IsInGrid())
              {
                  transform.position += new Vector3(-1, 0, 0);
              }
              else
              {
                  UpdateGameBoard();
                  SoundManager.Instance.PlayOneShot(SoundManager.Instance.shapeMove);
              }
          }
  
          if (Input.GetKeyDown("s") || Time.time - lastMoveDown >= Shape.speed)
          {
              transform.position += new Vector3(0, -1, 0);
  
              if (!IsInGrid())
              {
                  transform.position += new Vector3(0, 1, 0);
  
                  bool rowDeleted = GameBoard.DeleteAllFullRows();
  
                  if(rowDeleted)
                  {
                      GameBoard.DeleteAllFullRows();
                  }
  
                  enabled = false;
  
                  FindObjectOfType<ShapeSpawner>().SpawnShape();
  
                  SoundManager.Instance.PlayOneShot(SoundManager.Instance.shapeStop);
              }
              else
              {
                  UpdateGameBoard();
                  SoundManager.Instance.PlayOneShot(SoundManager.Instance.shapeMove);
              }
              lastMoveDown = Time.time;
  
          }
  
          if (Input.GetKeyDown("w"))
          {
              transform.Rotate(0, 0, 90);
  
              if (!IsInGrid())
              {
                  transform.Rotate(0, 0, -90);
              }
              else
              {
                  UpdateGameBoard();
                  SoundManager.Instance.PlayOneShot(SoundManager.Instance.rotateSound);
              }
  
          }
  
          if (Input.GetKeyDown("e"))
          {
              transform.Rotate(0, 0, -90);
  
              if (!IsInGrid())
              {
                  transform.Rotate(0, 0, 90);
              }
              else
              {
                  UpdateGameBoard();
                  SoundManager.Instance.PlayOneShot(SoundManager.Instance.rotateSound);
              }
  
          }
      }
  
      public bool IsInGrid()
      {
  
          foreach (Transform childBlock in transform)
          {
              Vector2 vect = RoundVector(childBlock.position);
  
              if(!IsInBorder(vect))
              {
                  return false;
              }
  
              if(GameBoard.gameBoard[(int)vect.x, (int)vect.y] != null && GameBoard.gameBoard[(int)vect.x, (int)vect.y].parent != transform)
              {
                  return false;
              }
          }
  
          return true;
      }
  
      public Vector2 RoundVector(Vector2 vect)
      {
          return new Vector2(Mathf.Round(vect.x), Mathf.Round(vect.y));
      }
  
      public static bool IsInBorder(Vector2 pos )
      {
          return ((int)pos.x >= 0 && (int)pos.x <= 9 && (int)pos.y >= 0);
      }
  
      public void UpdateGameBoard()
      {
          for(int y = 0; y<20; ++y)
          {
              for(int x = 0; x<10; ++x)
              {
                  if(GameBoard.gameBoard[x,y] != null && GameBoard.gameBoard[x, y].parent == transform)
                  {
                      GameBoard.gameBoard[x, y] = null;
                  }
              }
          }
  
          foreach(Transform childBlock in transform)
          {
              Vector2 vect = RoundVector(childBlock.position);
              GameBoard.gameBoard[(int)vect.x, (int)vect.y] = childBlock;
  
              //Debug.Log("Cube at" + vect.x + " " + vect.y);
          }
      }
  
  }
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

176 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

Related Questions

set the speed in respect of time 2 Answers

Play whole animation over specified time via script 1 Answer

RPG Action Progress Bar always same speed 1 Answer

Question about navMeshAgent speed and the "Time" variable 1 Answer

Animations Problem 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