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 /
  • Help Room /
avatar image
0
Question by Milenchy · Sep 22, 2016 at 12:04 PM · loopfreeze

Unity freezes when adding a GameObject to a List

Since 3 days ago Unity started freezing after adding a GameObject to a List. I haven't changed anything in that particular code, so I have no idea why this is happening:

  private IEnumerator HandleSelectedCubes() {
     if (InputManager.instance.selectedCube != null && !selectedCubes.Contains(InputManager.instance.selectedCube)) {
         if (oldSelectedCube == null) {
             selectedCubes.Add(InputManager.instance.selectedCube); // This is where it crashes
             oldSelectedCube = InputManager.instance.selectedCube;
         } else if (InputManager.instance.selectedCube.name != oldSelectedCube.name) {
             ClearSelectedCubes();
             selectedCubes.Add(InputManager.instance.selectedCube);
             oldSelectedCube = InputManager.instance.selectedCube;
             InputManager.instance.selectedCube = null;
         }

         AudioManager.instance.PlayCubeSelectSound();

         if (selectedCubes.Count == 4) {
             InputManager.instance.allowCubeSelection = false;
             moveDone = true;
             yield return new WaitForSeconds(0.07f);

             var destroySelectedCubes = true;

             foreach (var cube in selectedCubes.ToList()) {
                 if (cube != null && cube.tag == "Lightning")
                     destroySelectedCubes = false;
             }

             foreach (var cube in selectedCubes.ToList()) {
                 if (cube != null) {
                     cube.GetComponent<Cube>().hasGivenShard = true;
                     CubeAbilities.instance.TriggerSpecialCubeAbility(cube);

                     if (destroySelectedCubes) {
                         DestroyCube(cube);
                         ClearSelectedCubes();
                     }
                 }
             }

             InputManager.instance.playerMovesCount++;
             AudioManager.instance.PlayCubeDisappearSound();
             ShardManager.instance.AddSessionShards(4);
         }
     }
 }

InputManager:

   private void RegisterTouchInputs() {
         if (!IsScreenTouched() || !allowCubeSelection) return;

         var touch = Input.GetTouch(0);

         worldTouchPoint = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
         var radius = 0.1f;
         var allHits = Physics2D.CircleCastAll(worldTouchPoint, radius, Vector2.zero);

         // Find closest collider that was touched
         var closestDist = Mathf.Infinity;
         GameObject closestObject = null;

         foreach (var hit in allHits) {
             // Record the object if it's the first one we check, or is closer to the touch point than the previous
             if ((closestObject == null || Vector2.Distance(closestObject.transform.position, worldTouchPoint) < closestDist) && hit.collider.transform.parent.tag != "Indestructible") {
                 closestObject = hit.collider.gameObject;
                 closestDist = Vector2.Distance(closestObject.transform.position, worldTouchPoint);
             } else {
                 return;
             }

             //If there isn't an old cube or if the touch phase has just started, select the cube
             //Or if the touch phase has started and is moving (the user is holding and selecting cubes) and previously selected cubes are the same as this one, select the cube
             if ((CubeBehavior.instance.oldSelectedCube == null || touch.phase == TouchPhase.Began ||
                 (touch.phase == TouchPhase.Moved && CubeBehavior.instance.oldSelectedCube != null && CubeBehavior.instance.oldSelectedCube.name == closestObject.name))) {
                 CubeBehavior.instance.selectedCube = closestObject;
             } //If the touch phase has started and is moving (the user is holding and selecting cubes) but previously selected cubes are the same as this one, ignore the cube
             // This is so that it doesn't break the chain if the user goes over a differently colored cube
             else if (touch.phase == TouchPhase.Moved && CubeBehavior.instance.oldSelectedCube != null && CubeBehavior.instance.oldSelectedCube.name != closestObject.name) {
                 return;
             }
         }
     }
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 Bunny83 · Sep 22, 2016 at 12:42 PM 0
Share

Is Input$$anonymous$$anager.instance.selectedCube a property or a field? If it's a property, does it have any sideeffects or is it just a wrapper for a backing field?

Also i don't get your logic here. At the moment it seems impossible to select more than two cubes (at least when "ClearSelectedCubes" actually clears the "selectedCubes" List).

Furthermore your two foreach loops also seems a bit strange. Why do you call "ClearSelectedCubes" for every object in the list that isn't null and isn't tagged "Lightning"? $$anonymous$$eep in $$anonymous$$d that if all objects in your list are tagged "lightning" or somehow null (maybe because they have been destroyed) you won't clear the list at all.

It's hard to deter$$anonymous$$e the use of all this.

Just to be sure: By Unity "Hangs" you really mean it freezes like it's caught in an infinite loop?

The provided information is not enough to figure out what went wrong here.

avatar image Milenchy Bunny83 · Sep 22, 2016 at 01:20 PM 0
Share

It's a field. I edited my post and put the Input$$anonymous$$anager code as well.

If a selected object has the same name as the old one, it gets added to the list. If it doesn't, the list gets cleared and it starts from the beginning.

In some cases the objects and lists don't need to be destroyed/cleared

It freezes, as in impossible to interact with.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SohailBukhari · Sep 22, 2016 at 12:19 PM

@username What is selectedCube in your code ?? Are you sure inputmanager instance founded when you add selectedCube in list ??

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

54 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

Related Questions

Why does this code freeze unity? 0 Answers

Unity freezes. I dont know why... 3 Answers

loop causes freeze 0 Answers

How many instatiates per frame is too much? 0 Answers

Get axe/arrow to stick to object 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