- Home /
c# void Update() is only working once
This code\/ is supposed to make mesh and update the vertices every frame but it doesn't so I put a test var and in update made it test += 1;
but it only counts to 1 so update is only called once for some reason.
void Update()
{
GetComponent<MeshFilter>().mesh = CrateMeshChunk();
GetComponent<MeshCollider>().mesh = CrateMeshChunk();
}
Mesh CrateMeshChunk()
{
V1 = transform.InverseTransformPoint(Me.transform.position);
V2 = transform.InverseTransformPoint(NeighbourTop.transform.position);
V3 = transform.InverseTransformPoint(NeighbourLeft.transform.position);
V4 = transform.InverseTransformPoint(NeighbourLeft.GetComponent<Neighbour>().NeighbourTop.transform.position);
Mesh mesh = new Mesh();
Vector3[] vertices = new Vector3[]
{
V1,V2,V3,V4,
};
Vector2[] uv = new Vector2[]
{
UV1,UV2,UV3,UV4,
};
int[] triangles = new int[]
{
0, 1, 2,
2, 1, 3,
};
mesh.vertices = vertices;
mesh.uv = uv;
mesh.triangles = triangles;
return mesh;
}
Does another script in your project also update as expected? Does this class inherit from monobehavior? If so, then there's something wrong with your updating.
Its for all scripts and they do inherit monobehavior.
Put another script in the same scene, and see if it updates correctly. Does it? is this the only script that does not Update? If so, what is different about this script? The way to solve this is to continue commenting out parts of this script you've posted until you find a version that updates correctly -- then you'll find your problem, I think.
Ok, fair enough, but now how am I supposed to get credit for answering your question?
I made this script and it only goes to 1.
using UnityEngine;
using System.Collections;
public class Test : $$anonymous$$onoBehaviour
{
public int test;
void Update ()
{
test += 1;
}
}
So make sure first before anything else, that the GameObject is active, that the component is not being removed/destroyed and that it is active.
Once we know of that, we can go further.
The GameObject is active. The component is not being removed and it is active.
Answer by Catlard · May 07, 2015 at 06:13 AM
The script in the comment above works in my editor -- it updates test, and the number gets higher than 1. I suspect that your editor preferences have become weird, or something. My advice is to either reset your unity to the default settings and see if this still happens, or reinstall Unity.
EDIT: You could also try putting your script in a new project. If it works in the new project, then you could try reimporting all assets. If it doesn't work in a fresh project, then your copy of Unity is broken. If it does work in a new project, I suspect your project is broken somehow, and you could try migrating things from your old project until you find the broken bit.
EDIT 2: Had a look at your project. The problem was that your program was crashing on these lines:
V1 = transform.InverseTransformPoint(Me.transform.position);
V2 = transform.InverseTransformPoint(NeighbourTop.transform.position);
V3 = transform.InverseTransformPoint(NeighbourLeft.transform.position);
V4 = transform.InverseTransformPoint(NeighbourLeft.GetComponent<Neighbour>().NeighbourTop.transform.position);
It was crashing there because none of the "Neighbor" objects were assigned at that point, so it crashed the program and the editor paused. The reason it paused is because your "console" window had "Error Pause" checked.
Was this the problem?
Also, consider parenting your Voxels to the object that created them when you instantiate them, to keep your hierarchy tidy.
I there a way to pause the scene in code because I think thats what it's doing.
I would just redownload it. There's no way to pause the editor from code unless you're running an editor script, which I suspect you are not.
Also this:
http://askubuntu.com/questions/17610/how-do-i-reset-my-unity-configuration
$$anonymous$$y internet doesn't recap for a week so redownloading's not an option right now. No Im not using an editor script but I used // to hide the GetComponent<$$anonymous$$eshFilter>().mesh = Crate$$anonymous$$eshChunk(); GetComponent<$$anonymous$$eshCollider>().mesh = Crate$$anonymous$$eshChunk();
and it dosn't pause now so something in $$anonymous$$esh Crate$$anonymous$$eshChunk()
is pausing the scene.
Your answer

Follow this Question
Related Questions
Calling a method once in update 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Pause Menu Text Not Rendering 0 Answers
Is there a way to prevent the use of Update/FixedUpdate in child scripts? 2 Answers