Why does this have to be in an update method?

All I wanted to do was move the centre of mass of a rigid body, but it wouldn’t work unless I put it in an update, like this.

using System.Collections;

public class CoM : MonoBehaviour {


void Update() {
        rigidbody.centerOfMass = new Vector3(0, 0, -2);
    }
}

I just don’t see why you can’t put it in a custom method like “void MoveCenter()”.

Btw I’m learning Unity3d and C# at the same time.

1 Answer

1

instead of placing it in Update(), place it in Start()

void Start() {
   rigidbody.centerOfMass = new Vector3(0, 0, -2);
}

Update() gets called every frame, Start() only once in the beginning. If you would place it in MoveCenter() it would never get called.

Ah. Thanks. I guess the Unity Engine doesn't call custom methods. That's why we have start?

If you want custom methods to be called, you call them yourself from Start(),Awake(),Update().. depends on what behaviour you want. If I helped you out, please click the check-mark on the left of my answer. :)

Yes thanks. I understand how this works now.

Unity does not directly call custom methods, because it doesn't even know they exist! You have to call them yourself.

Syclamoth - Yes I get it now, finally! It makes sense. I didn't before - I was just doing things parrot fashion, but now things are starting to fall into place.