- Home /
Unity Editor Freezes- Help! Urgent!
Hello. I'm currently working on a script that slows time using Time.timeScale and shows up an arrow with SpriteRenderer, and then, rotate it and hide his sprite again after x seconds, putting the time back to normal. I think it should work fine, but unity, when that functoin is called, freezes and I have to open windows task manager to close it. An answer would be apreciated, thanks!
[script]
using System.Collections; using System.Collections.Generic; using UnityEngine; using CnControls; using Random = UnityEngine.Random;
public class ButtonTimerAndPower : MonoBehaviour { //PowerUps public string[] powers;
//Sprite varibles
public UnityEngine.UI.Image image;
public bool ready = false;
//Bomb PowerUp Variables
bool end = false;
public SpriteRenderer crosshairSprite;
public Transform rotationPoint;
public float rotateSpeed;
int powerID;
//Cooldown Sprites
public Sprite _1;
public Sprite _2;
public Sprite _3;
public Sprite _4;
public Sprite _5;
//End of ability countdown
public Sprite _1end_;
public Sprite _2end_;
public Sprite _3end_;
//PowerUp Button Sprites
public Sprite bombSprite;
public Sprite slowMotionSprite;
void Start()
{
StartCoroutine("resetTimer");
}
public void onCLick()
{
if (!ready)
{
return;
}
else if (ready)
{
ready = false;
powerUps();
}
}
public bool onClick2(bool ready)
{
if (ready)
{
return true;
}
else
{
return false;
}
}
IEnumerator resetTimer()
{
Debug.Log("RESET TIMER");
image.sprite = _5;
yield return new WaitForSeconds(1);
image.sprite = _4;
yield return new WaitForSeconds(1);
image.sprite = _3;
yield return new WaitForSeconds(1);
image.sprite = _2;
yield return new WaitForSeconds(1);
image.sprite = _1;
yield return new WaitForSeconds(1);
powerID = Random.Range(0, 2);
if (powers[powerID] == "Bomb")
{
image.sprite = bombSprite;
}
else if (powers[powerID] == "SlowMo")
{
image.sprite = slowMotionSprite;
}
ready = true;
}
void powerUps()
{
StopCoroutine("resetTimer");
if (powers[powerID] == "Bomb")
{
bomb();
}
else if (powers[powerID] == "SlowMo")
{
StartCoroutine("slowMo");
}
}
//POWERUPS METHODS
void bomb()
{
Debug.Log("1");
end = false;
Debug.Log("2");
Time.timeScale = 0.25f;
Debug.Log("3");
Time.fixedDeltaTime = 0.02f * Time.timeScale;
Debug.Log("4");
crosshairSprite.enabled = true;
Debug.Log("5");
while (!end)
{
rotationPoint.Rotate(CnInputManager.GetAxis("Horizontal") * rotateSpeed, 0, 0);
}
Debug.Log("6");
crosshairSprite.enabled = false;
Debug.Log("7");
Time.timeScale = 1f;
Debug.Log("8");
StartCoroutine("resetTimer");
}
IEnumerator slowMo()
{
Debug.Log("Neka's got 50% slower because he is dumb!!!");
Time.timeScale = 0.5f;
Time.fixedDeltaTime = 0.02f * Time.timeScale; //Smothen Slow Motion
image.sprite = _3end_;
yield return new WaitForSeconds(0.5f); //Wait 1 second (due to timescale): second * timescale
image.sprite = _2end_;
yield return new WaitForSeconds(0.5f);
image.sprite = _1end_;
yield return new WaitForSeconds(0.5f);
Time.timeScale = 1f;
StopCoroutine("powerUps");
StartCoroutine("resetTimer");
}
IEnumerator timerOfBomb()
{
yield return new WaitForSeconds(0.75f); //Wait 3 seconds: 3* 0.25
end = true;
}
}
[/script]
$$anonymous$$aybe put crosshairSprite before and after timescales?
Answer by tanoshimi · Jun 22, 2017 at 05:38 AM
You've got an infinite loop right here - nothing ever sets end to true:
while (!end)
{
rotationPoint.Rotate(CnInputManager.GetAxis("Horizontal") * rotateSpeed, 0, 0);
}
Your answer
Follow this Question
Related Questions
"Only assignment, call, increment, decrement and new object expressions can be used as statements" 1 Answer
Rotatearaound a moving object 1 Answer
How do I make a crouch script unity 3d 1 Answer
[Solved] Help with a simple endless level generator. 1 Answer
[SOLVED] How to still move the character but with different keys? 0 Answers