Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 ItsJacGaming · Aug 20, 2021 at 02:28 PM · scripting problembeginnerplay

Why does this script sometimes not complete its task?

First of all, I am sorry for the huge code dump. In my endless testing I have created something that needs to be refactored badly. I am still very new to programming and due to low funds I am somewhat self taught.
|
I Have attached the code and video link below because I have been trying to create a bubble shooter game similar to Bubble Witch Saga. At the moment The program works but taking what ever bubble I click on and then it checks the surrounding bubbles to find any that share the same colour, and then proceeds to check the newly selected bubbles for any more to match with before finally deleting them.
|
I don't understand why sometimes it will leave some of the bubbles behind and why sometimes it wont select from the end on the 'row' but will work if i click on the middle bubble. Does anyone know what could be causing this?
|
Please find the video here: https://youtu.be/LA_LjN0Jovg
|
Below is the script for this program:
|

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LevelManager : MonoBehaviour
 {
     public GameObject[] anchorPoints; // An Array Containing all the point on which the bubbles will spawn. Set in Inspector
     public GameObject bubble; //A variable containing a reference to the Bubble Prefab that will be Instantiated. Set in Inspector
     public List<GameObject> triggeredBubbles = new List<GameObject>(); // Creates an empty list called triggeredBubbles.
 
 
     // Start is called before the first frame update
     void Start()
     {
         for (int i = 0; i < anchorPoints.Length; i++) // for each item in the anchorPoints array:
         {
             Vector3 pos = anchorPoints[i].transform.position; //Assign the anchor point's position to a temp variable called pos
 
             GameObject newBubble = Instantiate(bubble, pos,Quaternion.identity); //create a clone of the bubble prefab, at the position currently in pos with no changes to rotation. And assign the new object to the variable newBubble
             
             newBubble.name = ("Bubble " + i); //Renames the object held in newBubble to 'Bubble x', where x is its location in the anchorPoint array
             
             SetColour(newBubble); //Run the SetColour Function passing the current newBubble through.
         }
     }
 
     // Update is called once per frame
     void Update()
     {
         //Player Controller ################################################################################3
         if (Input.GetMouseButtonDown(0))
         {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit))
             {
                 if (hit.collider.tag != null)
                 {
                     CheckConnected(hit.collider.gameObject);
                 }
             }
 
         }
     }
 
     // MY FUNCTIONS ######################################################################################3
 
     public void SetColour(GameObject newBubble)
     {
         int id = Random.Range(0, 4);
         if (id == 0)
         {
             newBubble.GetComponent<MeshRenderer>().material.color = Color.red;
             newBubble.tag = "red";
         }
         if (id == 1)
         {
             newBubble.GetComponent<MeshRenderer>().material.color = Color.blue;
             newBubble.tag = "blue";
         }
         if (id == 2)
         {
             newBubble.GetComponent<MeshRenderer>().material.color = Color.green;
             newBubble.tag = "green";
         }
         if (id == 3)
         {
             newBubble.GetComponent<MeshRenderer>().material.color = Color.magenta;
             newBubble.tag = "magenta";
         }
     }
 
     public void CheckConnected(GameObject newBubble)
     {
         
         int numberOfBubbles = 0;
         Collider[] attached = Physics.OverlapSphere(newBubble.transform.position, 0.55f);
 
         foreach (Collider col in attached)
         {
             string colliderName = col.name;
             if (col.tag == newBubble.tag)
             { 
                 if (triggeredBubbles.Contains(col.gameObject) == false) { triggeredBubbles.Add(col.gameObject); }
                 else { return; }
                 CheckConnected(col.gameObject);
                 numberOfBubbles += 1;
             }
         }
 
 
         if (triggeredBubbles.Count >= 3)
         {
             DestroyTriggedBubbles();
         }
         else
         {
             triggeredBubbles = new List<GameObject>();
         }
     }
 
     private void DestroyTriggedBubbles()
     {
         foreach(GameObject bubble in triggeredBubbles)
         {
             Destroy(bubble);
         }
         triggeredBubbles = new List<GameObject>();
     }
 
     
     }
 

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 Slaesh · Aug 20, 2021 at 06:15 PM 0
Share

Are you sure you wan to use "return" on 85 line when iterating through bubbles you found? I suppose it would be better to add all nearby bubbles to the list (with the correct tag) and then use recursive thing for all of them? Because now it seems like if you have circle of bubbles, for example, your algorithm could just go away from the circle center without adding some bubbles to the list.

avatar image ItsJacGaming Slaesh · Aug 20, 2021 at 11:12 PM 0
Share

Hey @Slaesh, I had thought it might be something to do with that. Do you have any idea what else I should you in its place?
!
If I try to run it without return, I get an infinite loop, and Unity hangs

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SunnyChow · Aug 23, 2021 at 03:35 AM

This is what i see...

  Collider[] attached = Physics.OverlapSphere(newBubble.transform.position, 0.55f);

Let's say it gets an Array of 3 balls : [(matched), (not matched), (matched)]

When checking the 1st ball, it's matched, so the ball is added to the List. When checking the 2nd ball, it doesn't match, so the function returns. And the function doesn't check the third ball.

Here is an idea. You make two List: checkList and matchList. You make a while loop that always check the first ball in checkList and then remove the first ball in checkList. if it's a match, add the ball to matchList, and do Physics.OverlapSphere() to add new balls to checkList (but make sure the new balls don't already exist in checkList and matchList)

EDIT If you want more improvement, maybe you should make the game based on a 2d Array, instead of checking ball via the physics engine. Also, using Queue for checkList maybe better.

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

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

Related Questions

I want to have a cannon that infinitely fires after a certain number of time but my script crashes unity 1 Answer

How do I make my joint slerp from 1 rotation to another? 1 Answer

X is a variable but a type was expected 1 Answer

I Have made a movement script for my pacman sprite. But animations keep playing even when there is no input. ans when i drag my sprite around in the scene it keeps trying to return to its original position. 0 Answers

How to always have collision always be on top. 1 Answer


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