Search Unity

swapping multiple components

Discussion in 'Scripting' started by Innovine, Oct 23, 2018.

  1. Innovine

    Innovine

    Joined:
    Aug 6, 2017
    Posts:
    522
    I have a player who moves due to several components; a rigidbody, some joints, and some scripts which read input and add forces. There are some tweaked PID controllers, etc.

    Now I would like, while the game is running, to swap out these components with ones that implement a different type of movement, lets say flying, or parachuting.

    I know how to add and remove components from c#, but I am not sure how to best go about handling the inspector values which have been carefully tweaked. I could probably put all the movement modes into one big component with tons of variables, but are there other, better ways?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You'd be better off putting these different combinations on different GameObjects that are a child of the main one, and activating/deactivating those. It's easier to sync a few properties like position than to recreate entire precisely tuned components.
     
  3. Innovine

    Innovine

    Joined:
    Aug 6, 2017
    Posts:
    522
    That could easily work for me, yep! Thanks!