Search Unity

Showcase Neat extension for modifying a Vector's component

Discussion in 'Scripting' started by mody_snowman, Sep 13, 2021.

  1. mody_snowman

    mody_snowman

    Joined:
    Apr 15, 2021
    Posts:
    1
    Hi,
    You know how you have to get a position from a transform to modify it and that takes 3 lines (usually)?
    Code (CSharp):
    1. Vector3 position = transform.position;
    2. position.x = 10;
    3. transform.position = position;
    Here's a very quick code snippet (a vector extension) that will allow you to do this:
    Code (CSharp):
    1. transform.position = transform.position.Modify(x: 10);
    Just place the following code in a file anywhere in your project and you'll be able to do so! Works with Vector2, Vector3, and Vector4.
    https://gist.github.com/modyari/e53cefad97aebeb9a290504206a7fc61

    Cheers,
     
    mgear likes this.