- Home /
Coroutine in Extension Method
I wrote an extension method to make an object move over time. The simplified version is as follows:
//This script gets attached to the GameObject
[RequireComponent(typeof(Rigidbody2D)]
public class MyBehaviour : MonoBehaviour
{
void Start()
{
rigidbody2D.MoveOverTime();
}
void Update(){
float newX = rigidbody2D.position.x;
float newy = rigidbody2D.position.y;
Vector2 newPosition = new Vector2(newX, newY);
rigidbody2D.MovePosition(newPosition); //Never called
}
}
=========================================================================================
//This script does not get attached to anything
public static class Rigidbody2DExtension
{
public static void MoveOverTime(this Rigidbody2D 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));
}
}
=========================================================================================
However, while my object is moving, it won't do anything else. I am assuming I need to throw a Coroutine in there somewhere, but I cannot figure out where! How can I go about making this script work while still allowing for other code to run on the gameObject?
Answer by Bunny83 · Nov 16, 2014 at 08:05 PM
Shouldn't that class look like this:
public static class Rigidbody2DExtension
{
public static void MoveOverTime(this Rigidbody2D rigidbody2D)
{
rigidbody2D.gameObject.addComponent<MoveOverTimeComponent>();
}
}
edit
In general for such methods it might be a good idea to return the attached component so the caller can do something with it:
public static MoveOverTimeComponent MoveOverTime(this Rigidbody2D rigidbody2D)
{
return rigidbody2D.gameObject.addComponent<MoveOverTimeComponent>();
}
That way you can use it like this for example:
Destroy(rigidbody2D.MoveOverTime(), 5);
Which would add the component and destroy it after 5 seconds.
@Bunny83 Yes, thank you. I updated it. But that is not my issue...that is just some simplified code I put to explain my issue. The issue is that my Update()
method in $$anonymous$$yBehaviour
is never being called.
@$$anonymous$$: Uhm, the "$$anonymous$$yBehaviour" has not much to do with the "$$anonymous$$oveOverTimeComponent" besides that it adds an instance to the same Gameobject. If your $$anonymous$$yBehaviour is still attached to your GameObject and it's still enabled you can be sure that Update will be called each frame.
Is it possible that you have "collapse" enabled in your console? That would hide new messages in the console.
@Bunny83 referring to your edit: perhaps. but i've actually got $$anonymous$$oveOverTimeComponent self-destructing after it finishes its movement in my real code. So I don't want to return a component when changes to null without their knowledge.
@Bunny83 I have attached the actual code to my question above. That may give you a better idea of my intent. The original code was just a simplified version.
@Bunny83 I just realized that my Update() method is being called...it was what I had in my update method that was failing. This is my actual Update() method, and it only runs the following method after its finished with the $$anonymous$$ovePositionInLine() call. However, a Debug.Log() in the same Update() method does print. I am so confused!
rigidbody2D.$$anonymous$$ovePosition(new Vector2(transform.position.x + 1, transform.position.y));