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.