4

I wrote an extension method to make an object move over time. It works well; however, because the object is performing that task over a period of time, it is ignoring all other calls, such as my update method. I am assuming I need to do something with a Coroutine, but I cannot figure out where it goes. How can I make the following code work without preventing other code (such as the Update() method) from running simulateneously? The simplified version is as follows:

==================================================================================

//The following script is attached to the GameObject
[RequireComponent(typeof(Rigidbody2D)]
public class MyBehaviour : MonoBehaviour
{
    void Start()
    {
        rigidbody2D.MoveOverTime();
    }

    void Update(){
        rigidbody2D.MovePosition(transform.position.x + 1, transform.position.y);
    }
}

==================================================================================

//The following script is not attached to anything
public static class Rigidbody2DExtension
{
    public static void MoveOverTime(this Rigidbody2D rigidbody2D)
    {
       gameObject.addComponent<MoveOverTimeComponent>();
    }
}

[RequireComponent(typeof(Rigidbody2D)]
class MoveOverTimeComponent : MonoBehaviour
{
    void Update(){
        MovePositionByALittleBit();
    }

    void MovePositionByALittleBit(){
        float x = transform.position.x + 1;
        float y = transform.position.y;
        rigidbody2D.MovePosition(new Vector2(x, y));
    }
}
user3071284
  • 6,955
  • 6
  • 43
  • 57
Evorlor
  • 7,263
  • 17
  • 70
  • 141
  • Why would your `Update` method(s) be called? They're never invoked as far as I can see. – Kirk Woll Nov 16 '14 at 19:40
  • @KirkWoll They are called automatically as an extension of MonoBehaviour – Evorlor Nov 16 '14 at 19:44
  • How? You aren't overriding the method and it's private. – Kirk Woll Nov 16 '14 at 19:45
  • @KirkWoll Honestly, I don't know. It is just a Unity thing. `Update()` is just called every frame when extending `MonoBehaviour`. `Start()` is called on creation. Creation of a `MonoBehaviour` happens when `AddComponent()` is called (like `MoveOverTimeComponent`) or it is attached in the engine (like `MyBehavior`). – Evorlor Nov 16 '14 at 19:54

1 Answers1

3

Both your Updates() are running. In fact, all the Updates() of all the MonoBehaviours will run.

Assuming that gameObject.addComponent<MoveOverTimeComponent>(); actually refers to a GameObject (which is not clear by the limited code you posted) then your problem is trying to move the same GameObject on two different Update() functions.

apxcode
  • 7,696
  • 7
  • 30
  • 41
  • That is my problem, thank you! I am trying to move the same `GameObject` on two different `Update()` functions. Any idea on how to allow this? – Evorlor Nov 16 '14 at 21:32
  • You are already moving, it just happens that you move in the same direction by same exact amount. Make one of the `Updates()` move only on the `y-axis` and the other on the `x-axis` so you can see the drastic change. – apxcode Nov 16 '14 at 21:36
  • I have made a brand new GameObject. It has nothing except for 2 scripts. One does this: Vector2 newPosition = new Vector2(transform.position.x + 1, transform.position.y); rigidbody2D.MovePosition(newPosition); The other does this: Vector2 newPosition = new Vector2(transform.position.x, transform.position.y + 1); rigidbody2D.MovePosition(newPosition); Only one of them is being called. I am going to need to do some research on this. – Evorlor Nov 16 '14 at 21:38
  • This may be of interest to you. (Or maybe not. Who knows.) But if you are curious, this is my dilemma (which you helped me realize): http://answers.unity3d.com/questions/833269/move-gameobject-with-multiple-scripts.html – Evorlor Nov 16 '14 at 21:44
  • I'll check it out. It might be that `MovePosition` is taking over the movement of the rigid body. It might just be that, if you run it with `AddForce` then it does work. – apxcode Nov 16 '14 at 21:48