Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by $$anonymous$$ · Nov 16, 2014 at 05:57 PM · coroutineupdatecomponent

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Nov 16, 2014 at 08:08 PM 0
Share

@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.

avatar image Bunny83 · Nov 16, 2014 at 08:13 PM 0
Share

@$$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.

avatar image $$anonymous$$ · Nov 16, 2014 at 08:14 PM 0
Share

@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.

avatar image $$anonymous$$ · Nov 16, 2014 at 08:20 PM 0
Share

@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.

avatar image $$anonymous$$ · Nov 16, 2014 at 08:29 PM 0
Share

@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));
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[WORKAROUND] How to time pause in update (yield) 1 Answer

Checkibg Rigidbody2d and component problems 0 Answers

How to trigger udpate in editor mode only when I modify component parameters 1 Answer

Invoke repeating OR coroutine with WaitForSeconds? 1 Answer

Make a fade in/out inside 'upDate()' function 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges