Home > other >  Unity 3D: Unknown syntax errors: tuple, invalid tokens, type or namespace expected
Unity 3D: Unknown syntax errors: tuple, invalid tokens, type or namespace expected

Time:09-27

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;
    
}

}

enter image description here

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.

  1. 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;

  2. 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).

  3. vertices = new Vector3[(Mathf.Pow(2, nTimes - 1)) 2]; You must first declare vertices variable before using it Vector3 vertices. This assignment is at least weird, check following points.

  4. 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.

  5. vertices[0] again, you are using your Vector as an array. If you want to access a Vector3, do it by using vertices.x or vertices.y. If you want to create and access an array, go checkout how to do it.

Good luck on your learning path :)

  • Related