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<em>, vertexList _+ (vertexList[i + 1] - vertexList*) / 2 - vertexList[0]);*_</em>

//Debug.DrawRay(vertexList, vertexList + (vertexList[(i == 1) ? _players : i - 1] - vertexList*) / 2 - vertexList[0]);*

/vertexList.Add(vertexList + ((vertexList + (vertexList[i + 1] - vertexList) / 2 - vertexList[0]).normalized * areaSizeFactor));
triangleList.Add(i);
triangleList.Add(i + 1);
triangleList.Add(vertexList.Count - 1);
/_

/*
Debug.DrawLine(vertexList*, vertexList[i + 1]);*
Debug.DrawLine(vertexList[i + 1], vertexList + ((vertexList + (vertexList[i + 1] - vertexList) / 2 - vertexList[0]).normalized * _areaSizeFactor));
Debug.DrawLine(vertexList + ((vertexList + (vertexList[i + 1] - vertexList) / 2 - vertexList[0]).normalized * _areaSizeFactor), vertexList*);*

Debug.DrawLine(vertexList[i + 1], vertexList[i + 1] + ((vertexList + (vertexList[i + 1] - vertexList) / 2 - vertexList[0]).normalized * _areaSizeFactor));
Debug.DrawLine(vertexList + ((vertexList + (vertexList[i + 1] - vertexList) / 2 - vertexList[0]).normalized * areaSizeFactor), vertexList[i + 1] + ((vertexList + (vertexList[i + 1] - vertexList) / 2 - vertexList[0]).normalized * areaSizeFactor));
*/

Vector3 normalDir = (vertexList + (vertexList[i + 1] - vertexList*) / 2 - vertexList[0]).normalized; //*
* //*
Vector3 UpperI = vertexList + (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.

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.

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 Unity - Scripting API: Mesh 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.