• 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 gametimespeedscene-loadingtetris

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

increasing speed and time of spawn game object 0 Answers

2D Limit player speed 3 Answers

Time.deltaTime is 0 after LoadLevel 1 Answer

how to track object movement speed 2d 2 Answers

Different swipe speed between iPad2 & iPad3 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges