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 kederin · Mar 22, 2021 at 07:51 PM · scripting problemcrashunityeditorscripting beginner

Unity Editors crashes after using a button in scene

Hi,

My Unity Editor(2019.4.11f1) keeps crashing after I use a button in my scene. Now I don't know why but I'm pretty sure it has to do something with my Game Manager. I also tried using another version of the editor but I get the same problem. Here is my Game Manager Script:

 {
     public TextMeshProUGUI CounterText;
     public TextMeshProUGUI scoreText;
 
     private int turn = 0;
     public static int score;
     public int scoreValue;
 
     public bool isGameActive;
 
     public GameObject titleScreen;
     public Button startbutton;
 
     private void Start()
     {
        
     }
 
     private void OnTriggerEnter(Collider other)
     {
         turn += 1;
         CounterText.text = "Turn : " + turn + "/3";
 
         score += scoreValue;
         scoreText.text = "score: " + score;
     }
 
     public void startGame()
     {
         isGameActive = true;
         score = 0;
         turn = 0;
         titleScreen.gameObject.SetActive(false);
     }
 }

Here is the Game Manager's inspector: alt text

I'm making a title screen that i want to deactivate as soon as I click the start button, when I click the button the whole editor freezes. Can anyone make out what is causing that. I'll add the script for my button as well just in case:

     private Button startButton;
     private GameManager gameManager;
 
     // Start is called before the first frame update
     void Start()
     {
         startButton = GetComponent<Button>();
         gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
 
         startButton.onClick.AddListener(StartGameOnClick);
     }
 
     // Update is called once per frame
     void Update()
     {
 
     }
     void StartGameOnClick()
     {
         Debug.Log(" Game has started");
         gameManager.startGame();
     }
 
 }

Does anybody have a clue of why this is happening? Thanks in advance!

game-manager.png (31.9 kB)
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 toficofi · Mar 22, 2021 at 08:20 PM 0
Share

Is there any more relevant code to this? I don't see anything that immediately stands out. An editor freeze often means getting stuck in a loop.

Does it actually crash after freezing (aka, present the "Unity Editor has stopped working!" dialog), or does it just hang indefinitely?

avatar image kederin toficofi · Mar 22, 2021 at 11:39 PM 0
Share

@toficofi As soon as i click the button it just freezes and it stays that way, I even tried to wait like 15 $$anonymous$$utes but it was still frozen, I didn't get any dialog, there was just no way I could interact with the editor anymore.

I have some other scripts, one is the PlayerController and the other FollowPlayer, that's for the camera to follow to ball with a offset. Here's the PlayerController script:

 {
 
     public float speed;
     private float horizontalInput;
     private float verticalInput;
 
     
     //how many balls the player has
     public static float ballCount = 3;
     //player's score
     public static float score = 0;
 
     Rigidbody ballRb;
     // checks if the ball has been dropped
     bool isDropped;
 
     private GameManager gameManager;
 
 
     // Start is called before the first frame update
     void Awake()
     {
         ballRb = GetComponent<Rigidbody>();
         gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
         
     }
 
     // Update is called once per frame
     void Update()
     { while (gameManager.isGameActive)
         {
             //if the ball has not been dropped, use MoveBall method
             if (!isDropped)
             {
                 MoveBall();
             }
             // makes the ball drop with spacebar
             if (Input.GetKeyDown(KeyCode.Space))
             {
                 isDropped = true;
                 ballRb.useGravity = true;
             }
         }
     }
 
     //makes player move the ball with WASD
     void MoveBall()
     {
         verticalInput = Input.GetAxis("Vertical");
         horizontalInput = Input.GetAxis("Horizontal");
 
         transform.Translate(Vector3.forward * Time.deltaTime * speed * verticalInput);
         transform.Translate(Vector3.right * Time.deltaTime * speed * horizontalInput);
     }    
 }

I don't really think there is anything wrong with this script either but I could be overlooking something. Thanks for taking your time!

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by toficofi · Mar 23, 2021 at 01:16 AM

There's your problem:

 void Update()
      { while (gameManager.isGameActive)
          { ...

You can't use a while loop like this in your Update method because Unity runs on a single thread, which means it waits until the whole Update has finished executing before moving onto the next frame, and in this case gameManager.isGameActive stays true, and that Update() will never finish.

You are probably wanting to do this, instead:

  void Update()
      { if (gameManager.isGameActive)
          {
Comment
Add comment · Show 2 · 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
avatar image kederin · Mar 23, 2021 at 01:35 PM 0
Share

Yes, that has solved it. Looks like I misunderstood the concept of the while loop, I will take a better look at how to properly use it. Thanks a lot

avatar image toficofi kederin · Mar 23, 2021 at 02:07 PM 0
Share

No problem, best of luck!

avatar image
0

Answer by pKallv · Mar 22, 2021 at 11:52 PM

Check out the Unity log to find out what the problem is, you can find where it is here: https://docs.unity3d.com/Manual/LogFiles.html

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

228 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

How do I make a photography function? 0 Answers

Slowly increase joint limit over time 1 Answer

How to return control back to the original script? 1 Answer

weighted inventory system 1 Answer

Unity crashes on start up (Linux) 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