Forum begins after the advertisement:
[Part 21] Game freezing caused by RingEventData.cs
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [Part 21] Game freezing caused by RingEventData.cs
- This topic has 7 replies, 2 voices, and was last updated 2 weeks, 1 day ago by 정춤고 (ChoomGo).
-
AuthorPosts
-
September 18, 2024 at 11:07 pm #15850정춤고 (ChoomGo)Participant::
I added the code for this part and ran the build, but Unity itself keeps freezing. No error code appears, and it freezes immediately, so there doesn’t seem to be a way to solve it. Is there anyone who might be experiencing something similar to this?
Terence’s suggestion is as follows:
is causing this, you will eventually come across an object that, after deleting, will allow the game to continue running. Then you will know which script is causing this, and you can post it on the forums for us to have a look for you.
I did that and found that the issue is with
RingEventData.cs
.RingEventData.cs
1.57 KB • 44 extracted linesFormatting may be inconsistent from source.
using UnityEngine; [CreateAssetMenu(fileName = "Ring Event Data", menuName = "Datas/Event Data/Ring")] public class RingEventData : EventData { [Header("Mob Data")] public ParticleSystem spawnEffectPrefab; public Vector2 scale = new Vector2(1, 1); [Min(0)] public float spawnRadius = 10f, lifespan = 15f; public override bool Activate(PlayerStats playerStats = null) { // Only activate this if the player is present. if (playerStats) { GameObject[] spawns = GetSpawns(); float angleOffset = 2 * Mathf.PI / Mathf.Max(1, spawns.Length); float currentAngle = 0; foreach (GameObject g in spawns) { // Calculate the spawn position. Vector3 spawnPosition = playerStats.transform.position + new Vector3( spawnRadius * Mathf.Cos(currentAngle) * scale.x, spawnRadius * Mathf.Sin(currentAngle) * scale.y ); // If a particle effect is assigned, play it on the position. if (spawnEffectPrefab) Instantiate(spawnEffectPrefab, spawnPosition, Quaternion.identity); // Then spawn the enemy. GameObject s = Instantiate(g, spawnPosition, Quaternion.identity); // If there is a lifespan on the mob, set them to be destroyed. if (lifespan > 0) Destroy(s, lifespan); currentAngle += angleOffset; } } return false; } }
The code above is what part 21 is about. Below is the addition of a code that gathers in the middle with the help of chat GPT. The same freesing occurred in both scripts.
using UnityEngine; using System.Collections; using System.Collections.Generic; [CreateAssetMenu(fileName = "Ring Event Data", menuName = "Datas/Event Data/Ring")] public class RingEventData : EventData { [Header("Mob Data")] public ParticleSystem spawnEffectPrefab; public Vector2 scale = new Vector2(1, 1); [Min(0)] public float spawnRadius = 10f; [Header("Convergence Data")] [Min(0)] public float convergenceTime = 5f; [Range(0f, 1f)] public float convergenceRatio = 0.9f; [Min(0)] public float disappearDelay = 0.5f; public override bool Activate(PlayerStats playerStats = null) { // Only activate this if the player is present. if (playerStats) { GameObject[] spawns = GetSpawns(); float angleOffset = 2 * Mathf.PI / Mathf.Max(1, spawns.Length); float currentAngle = 0; List<GameObject> spawnedEnemies = new List<GameObject>(); foreach (GameObject g in spawns) { // Calculate the spawn position. Vector3 spawnPosition = playerStats.transform.position + new Vector3( spawnRadius * Mathf.Cos(currentAngle) * scale.x, spawnRadius * Mathf.Sin(currentAngle) * scale.y ); // If a particle effect is assigned, play it on the position. if (spawnEffectPrefab) Instantiate(spawnEffectPrefab, spawnPosition, Quaternion.identity); // Then spawn the enemy. GameObject s = Instantiate(g, spawnPosition, Quaternion.identity); spawnedEnemies.Add(s); currentAngle += angleOffset; } // Start the convergence coroutine playerStats.StartCoroutine(ConvergeAndDestroyEnemies(spawnedEnemies, playerStats.transform)); } return false; } private IEnumerator ConvergeAndDestroyEnemies(List<GameObject> enemies, Transform playerTransform) { float elapsedTime = 0f; while (elapsedTime < convergenceTime) { foreach (GameObject enemy in enemies) { if (enemy != null) { Vector3 directionToPlayer = playerTransform.position - enemy.transform.position; float distanceToMove = directionToPlayer.magnitude * (convergenceRatio * (elapsedTime / convergenceTime)); enemy.transform.position += directionToPlayer.normalized * distanceToMove * Time.deltaTime; } } elapsedTime += Time.deltaTime; yield return null; } // Wait for the specified delay before destroying yield return new WaitForSeconds(disappearDelay); // Destroy all enemies foreach (GameObject enemy in enemies) { if (enemy != null) { Destroy(enemy); } } } }
September 18, 2024 at 11:07 pm #15851정춤고 (ChoomGo)Participant::Unity doesn’t stop if I don’t put ring event data in Enemy spawner. Unity stops when I add it.
September 19, 2024 at 12:32 pm #15854TerenceKeymaster::Can you remove the line that starts the
ConvergeAndDestroyEnemies()
coroutine and see if that fixes the freezing? The issue seems to be that one of your loops are running forever.September 23, 2024 at 6:06 am #15897정춤고 (ChoomGo)Participant::Unity stops even when I remove the code and add a ring event. I don’t think this cs file is the cause of the stop. I’m just guessing maybe there’s another cause.
September 23, 2024 at 2:28 pm #15901TerenceKeymaster::How about if you remove the entire loop in
Activate()
? Does it still crash?public override bool Activate(PlayerStats playerStats = null) { // Only activate this if the player is present. if (playerStats) { GameObject[] spawns = GetSpawns(); float angleOffset = 2 * Mathf.PI / Mathf.Max(1, spawns.Length); float currentAngle = 0; List<GameObject> spawnedEnemies = new List<GameObject>();
foreach (GameObject g in spawns) { // Calculate the spawn position. Vector3 spawnPosition = playerStats.transform.position + new Vector3( spawnRadius * Mathf.Cos(currentAngle) * scale.x, spawnRadius * Mathf.Sin(currentAngle) * scale.y ); // If a particle effect is assigned, play it on the position. if (spawnEffectPrefab) Instantiate(spawnEffectPrefab, spawnPosition, Quaternion.identity); // Then spawn the enemy. GameObject s = Instantiate(g, spawnPosition, Quaternion.identity); spawnedEnemies.Add(s); currentAngle += angleOffset; } // Start the convergence coroutine playerStats.StartCoroutine(ConvergeAndDestroyEnemies(spawnedEnemies, playerStats.transform));} return false; }September 23, 2024 at 11:30 pm #15905정춤고 (ChoomGo)Participant::I modified it to the code above, and Unity stopped again. I’ll restore it to the original code and then look again.
September 24, 2024 at 12:32 pm #15909TerenceKeymaster::The other possibility is that the issue is with the
EventManager
script. Your issue looks like it is an issue with one of your loops though, which makes me think that thewhile
loop inConvergeAndDestroyEnemies()
is the most likely culprit.private IEnumerator ConvergeAndDestroyEnemies(List
enemies, Transform playerTransform) { float elapsedTime = 0f; while (elapsedTime < convergenceTime) { foreach (GameObject enemy in enemies) { if (enemy != null) { Vector3 directionToPlayer = playerTransform.position - enemy.transform.position; float distanceToMove = directionToPlayer.magnitude * (convergenceRatio * (elapsedTime / convergenceTime)); enemy.transform.position += directionToPlayer.normalized * distanceToMove * Time.deltaTime; } } elapsedTime += Time.deltaTime; yield return null; } // Wait for the specified delay before destroying yield return new WaitForSeconds(disappearDelay); // Destroy all enemies foreach (GameObject enemy in enemies) { if (enemy != null) { Destroy(enemy); } } } September 24, 2024 at 11:30 pm #15919정춤고 (ChoomGo)Participant::I deleted the displayed code. Still trying to add a code where monsters gather in the middle. Thank you for your kindness.
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: