I am trying to create a circle out of vertices based on certain parameters like subdivision or radius. This time I have been avoiding using tutorials as a crutch as I haven't been able to learn C# by not actually working with it. I finished this bit of code and got stuck on the console errors because I am a beginner with troubleshooting. Would any of you be able to help me understand what they mean?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class circleVerts : MonoBehaviour
{
private void Update () {
Generate();
}
private void Generate () {
GetComponent<MeshFilter>().mesh = mesh = new Mesh();
mesh.name = "Procedural Circle";
public int radiusR = 2;
public float nTimes = 1.0f; //# of subdivisions, cannot equal 0
public int numPlayers = 4;
public float thetaT = (Mathf.Pow(2, nTimes) / Mathf.Pow(numPlayers, nTimes)) * Mathf.PI;
vertices = new Vector3[(Mathf.Pow(2, nTimes - 1)) 2];
for (int i = 0; i <= vertices.length; i )
{
float x = Mathf.Cos(thetaT) * radiusR;
float y = Mathf.Sin(thetaT) * radiusR;
vertices[0] = new Vector3(0,0,0); //setting origin
vertices[1] = new Vector3(radiusR, 0, 0); //first vert is located at x-radius
vertices[i] = new Vector3(x, 0, y);
Gizmos.DrawSphere(vertices[i].position, 1);
}
mesh.vertices = vertices;
}
}
CodePudding user response:
Very glad you are trying to do things by yourself, but it seems you lack basic grasp of C# syntax. Maybe instead of tutorials of how to achieve something, try tutorials on language itself.
With that out of the way, let's look at your issue.
There's really no point of addressing every error as they are caused by wrong syntax, and the problems just cascade further. They create even more bizarre errors, which seemingly makes no sense, like you certainly didn't mean to use tuples. It will be better if I just explain what's wrong with the code and why.
GetComponent<MeshFilter>().mesh = mesh = new Mesh();
- do you want to create new Mesh, or get the component of an existing one? Probably the latter. Create Mesh variable, and assign to it the result of GetComponent method like this:Mesh mesh = GetComponent<MeshFilter>().mesh;
Access modifiers shouldn't be used inside a method. When the method ends, the variables will be gone, so marking them public wouldn't make much sense anyway. Either declare them inside class body, or remove access modifiers (public).
vertices = new Vector3[(Mathf.Pow(2, nTimes - 1)) 2];
You must first declare vertices variable before using itVector3 vertices
. This assignment is at least weird, check following points.You are trying to use
[]
like you were working with an array, while you create a new Vector3 not an array of Vector3. Moreover, Vector3 doesn't have a constructor that accepts only one argument. Decide whether you want to use a Vector or an array of Vectors.vertices[0]
again, you are using your Vector as an array. If you want to access a Vector3, do it by usingvertices.x
orvertices.y
. If you want to create and access an array, go checkout how to do it.
Good luck on your learning path :)