I'm getting some errors in the script how can i fix them ?
float size;
void Awake()
{
Mesh m = new Mesh();
m.name = "Scripted_Plane_New_Mesh";
m.vertices = [new Vector3(-size, -size, 0.01f), new Vector3(size, -size, 0.01f), new Vector3(size, size, 0.01f), new Vector3(-size, size, 0.01f)];
m.uv = [new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0)];
m.triangles = [0, 1, 2, 0, 2, 3];
m.RecalculateNormals();
GameObject obj = new GameObject("New_Plane_Fom_Script", MeshRenderer, MeshFilter, MeshCollider);
obj.GetComponent(MeshFilter).mesh = m;
}
First i'm getting error in every line that have '[' For example:
m.vertices = [new Vector3(-size, -size, 0.01f), new Vector3(size, -size, 0.01f), new Vector3(size, size, 0.01f), new Vector3(-size, size, 0.01f)];
The error is on the [ Invalid expression term '['
Then i'm getting errors on:
MeshRenderer, MeshFilter, MeshCollider and the last MeshFilter all errors are the same:
'MeshRenderer' is a type, which is not valid in the given context
This site is not really intended for "please fix my script" services.
You need to follow the current manual docs about meshes and how to create geometry on the fly.
You are currently using five and a half hear old Unity script (July 06, 2011) which is why you are getting errors (pre Unity 3.4.0 which was released July 26, 2011).
This will correct your errors (it does not do or show anything but it does create the mesh)
private $$anonymous$$eshFilter meshFilter;
public float size;
void Awake()
{
$$anonymous$$esh m = new $$anonymous$$esh();
m.vertices = new Vector3[4] {new Vector3(-size, -size, 0.01f), new Vector3(size, -size, 0.01f), new Vector3(size, size, 0.01f), new Vector3(-size, size, 0.01f)};
m.uv = new Vector2[4] {new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0)};
m.triangles = new int[6] {0, 1, 2, 0, 2, 3};
m.RecalculateNormals();
meshFilter = gameObject.GetComponent<$$anonymous$$eshFilter>();
if (meshFilter == null)
{
meshFilter = gameObject.AddComponent<$$anonymous$$eshFilter>();
}
// assign the mesh to a $$anonymous$$eshFilter
meshFilter.mesh = m;
}
Also, Take a look at this free asset made by Unity. It was made for Unity 3.5.6 or higher and you will get the standard Possible incompatibility with Unity 5
error when importing it but Unity will be able to import the scripts and the demos do work.
Answer by Mister-Mortal · Nov 28, 2016 at 02:32 PM
For the bracket ('[') part, instead of brackets you need to use curly brackets like this:
someArray = { firstValue, secondValue, thirdValue };
For second error, you need to use typeof() method like this:
GameObject obj = new GameObject("New_Plane_Fom_Script", typeof(MeshRenderer), typeof(MeshFilter), typeof(MeshCollider));
I think you don't even need the new[]
when initializing an array with values. Am I wrong?
You are right, thanks! Updated the answer.
Your answer
Follow this Question
Related Questions
Can someone please help me find out what wrong with my code. 1 Answer
How can i disable/enable slowly the blur effect using the blur script ? 0 Answers
How can I give each instance a enum mode from another script ? 0 Answers
How can i make the right choice of implementation of my shooting fire script ? 1 Answer
can i get help please 0 Answers