Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 moreira_dev · Jun 22, 2017 at 05:34 AM · c#androidscripting problemscripting beginner

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]

Comment
Add comment · Show 2
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
avatar image moreira_dev · Jun 17, 2017 at 12:51 PM 0
Share

PS: The part that bugs is when bomb() is called!

avatar image moreira_dev · Jun 19, 2017 at 11:18 PM 0
Share

$$anonymous$$aybe put crosshairSprite before and after timescales?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
  }
Comment
Add comment · Share
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

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

103 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

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


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