- Home /
My script doesn't let unity start
So, I have a script and when its active in the scene, Unity doesn't enter play mode. It gets stuck trying to enter, and I have to force it to close via task manager. Heres the code:
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class PlaygroundGenerator : MonoBehaviour {
[SerializeField]
private int players = 4;
[SerializeField]
private int stonesPerPlayer = 4;
[SerializeField]
private int areaSizeFactor = 4;
void Update ()
{
gameObject.GetComponent<MeshFilter>().mesh = GeneratePlaygroundMesh(players,stonesPerPlayer,areaSizeFactor);
}
public Mesh GeneratePlaygroundMesh(int _players,int _stonesPerPlayer,int _areaSizeFactor)
{
int sideSize = 3;
float angleStep = 360.0f / _players;
List<Vector3> vertexList = new List<Vector3>();
List<int> triangleList = new List<int>();
Quaternion quaternion = Quaternion.Euler(0.0f, angleStep, 0.0f);
// Make first triangle.
float radius = sideSize / 2f / Mathf.Cos((90f - angleStep / 2) * Mathf.Deg2Rad);
vertexList.Add(new Vector3(0.0f, 0.0f, 0.0f)); // 1. Circle center.
vertexList.Add(new Vector3(0.0f, 0.0f, radius)); // 2. First vertex on circle outline
//Debug.Log(radius);
vertexList.Add(quaternion * vertexList[1]); // 3. First vertex on circle outline rotated by angle)
// Add triangle indices.
triangleList.Add(0);
triangleList.Add(1);
triangleList.Add(2);
for (int i = 0; i < _players - 1; i++)
{
triangleList.Add(0); // Index of circle center.
triangleList.Add(vertexList.Count - 1);
triangleList.Add(vertexList.Count);
vertexList.Add(quaternion * vertexList[vertexList.Count - 1]);
}
for (int i = 0; i < vertexList.Count - 1; i++)
{
if(i != 0)
{
//Debug.DrawRay(vertexList[i], vertexList[i] + (vertexList[i + 1] - vertexList[i]) / 2 - vertexList[0]);
//Debug.DrawRay(vertexList[i], vertexList[i] + (vertexList[(i == 1) ? _players : i - 1] - vertexList[i]) / 2 - vertexList[0]);
/*vertexList.Add(vertexList[i] + ((vertexList[i] + (vertexList[i + 1] - vertexList[i]) / 2 - vertexList[0]).normalized * _areaSizeFactor));
triangleList.Add(i);
triangleList.Add(i + 1);
triangleList.Add(vertexList.Count - 1);
*/
/*
Debug.DrawLine(vertexList[i], vertexList[i + 1]);
Debug.DrawLine(vertexList[i + 1], vertexList[i] + ((vertexList[i] + (vertexList[i + 1] - vertexList[i]) / 2 - vertexList[0]).normalized * _areaSizeFactor));
Debug.DrawLine(vertexList[i] + ((vertexList[i] + (vertexList[i + 1] - vertexList[i]) / 2 - vertexList[0]).normalized * _areaSizeFactor), vertexList[i]);
Debug.DrawLine(vertexList[i + 1], vertexList[i + 1] + ((vertexList[i] + (vertexList[i + 1] - vertexList[i]) / 2 - vertexList[0]).normalized * _areaSizeFactor));
Debug.DrawLine(vertexList[i] + ((vertexList[i] + (vertexList[i + 1] - vertexList[i]) / 2 - vertexList[0]).normalized * _areaSizeFactor), vertexList[i + 1] + ((vertexList[i] + (vertexList[i + 1] - vertexList[i]) / 2 - vertexList[0]).normalized * _areaSizeFactor));
*/
Vector3 normalDir = (vertexList[i] + (vertexList[i + 1] - vertexList[i]) / 2 - vertexList[0]).normalized; //
//
Vector3 UpperI = vertexList[i] + (normalDir * _areaSizeFactor); //
Vector3 UpperIPlus1 = vertexList[i + 1] + (normalDir * _areaSizeFactor); //
//
vertexList.Add(UpperI); //
vertexList.Add(UpperIPlus1); // When I comment out
// this area, Unity
int UpperIIndex = vertexList.IndexOf(UpperI); // can run.
int UpperIPlus1Index = vertexList.IndexOf(UpperIPlus1); //
//
triangleList.Add(i); //
triangleList.Add(UpperIIndex); //
triangleList.Add(i + 1); //
//
triangleList.Add(UpperIIndex); //
triangleList.Add(UpperIPlus1Index); //
triangleList.Add(i + 1); //
}
}
Mesh mesh = new Mesh();
mesh.vertices = vertexList.ToArray();
mesh.triangles = triangleList.ToArray();
return mesh;
}
void OnValidate()
{
if (players < 3)
{
players = 3;
}
if (stonesPerPlayer < 4)
{
stonesPerPlayer = 4;
}
if (areaSizeFactor < stonesPerPlayer)
{
areaSizeFactor = stonesPerPlayer;
}
}
}
So , does this code run on your PC? If so, please tell me. If it doesn't but you understand why, tell me. PS: to run this, just put this code on an empty game object.
Answer by NoseKills · Jun 12, 2017 at 03:21 PM
You do have this in there
for (int i = 0; i < vertexList.Count - 1; i++)
{
if(i != 0)
{
...
vertexList.Add(UpperI);
vertexList.Add(UpperIPlus1);
vertexList.Count increases by 2 every loop and i increments by 1... You'll never hit the end condition.
Answer by gameplay4all · Jun 12, 2017 at 10:05 AM
I think it does run, but terribly slow. I don't see an infinite loop in you code (that's often the problem when the editor gets stuck) so this function is just taking an enormous time to calculate. I haven't had a good look at your code but this mesh doesn't seem to change every frame so why not:
move the function call to Start instead of Update
Cache the MeshFilter component and remove the GetComponent (slight performance boost).
Pass the MeshFilter to the GeneratePlaygroundMesh function and set it's vertices and triangles directly, saves the performance of generating a completely new mesh.
If you are continuously updating the mesh (so you keep it in Update) you need to call Mesh.Clear before each change, if you use the above method.
See https://docs.unity3d.com/ScriptReference/Mesh.html for some other tips.
I hope this solved your problem, and sometimes patience is the answer. I had occasions where my start function (before optimization of course) would take 30+ seconds but in the end the game would run.
I'm glad for your answer, and I'll try these, but patiece is not the answer(I waited ~1h).