- Home /
Time freeze
hy guys! in my game, when the player collides with an object, i want to display a "restart" screen(UI) and freeze the time. My problem is, whenever the player hits the object, restart screen appear, but the time doesnt freeze, only when they collide for the second time. here is my player's code.`using System.Collections; using System.Collections.Generic; using UnityEngine;
public class controlls : MonoBehaviour {
public float speed = 600f;
public Joystick joystick;
public GameObject restart;
public GameObject UI;
float movementx = 0f;
private void Start() {
restart.SetActive(false);
}
void Update() {
movementx = joystick.Horizontal;
}
private void FixedUpdate() {
transform.RotateAround(Vector3.zero, Vector3.forward, movementx * Time.fixedDeltaTime * -speed);
}
private void OnTriggerEnter2D(Collider2D other) {
if (other.CompareTag("hexa")) {
restart.SetActive(true);
UI.SetActive(false);
Time.timeScale = 0;
}
}
}
`
Answer by darksider2000 · May 17, 2020 at 12:43 PM
Hi there,
This might be caused if one of your other game objects (UI or restart) is disabling/interfering with this code (controlls).
You should try putting the Time.timeScale line before you use the SetActive():
private void OnTriggerEnter2D(Collider2D other) {
if (other.CompareTag("hexa")) {
Time.timeScale = 0;
restart.SetActive(true);
UI.SetActive(false);
}
}
Or alternatively you could put the Time.timeScale line in the restart game object's code.
So then when you do restart.SetActive(true) it will run the restart code and do the timeScale manipulation.
Your answer
Follow this Question
Related Questions
How do I make colliders collide when Tİme Scale is zero? 1 Answer
Collision of linerenderer unity2d 1 Answer
Check whether any collision has occured to spawned game objects 0 Answers
How can I detect a collision between two clones and delete them both? 1 Answer
Unity2D: How to make my player not walk through walls? 1 Answer