Search Unity

Why do use of unassigned Rect and Vector2 behave differently?

Discussion in 'Scripting' started by Stevens-R-Miller, Mar 21, 2019.

  1. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    Visual Studio flags an error, "
    Use of unassigned local variable 'r'
    ," at Line 11. But not at Line 7. Both
    Vector2
    and
    Rect
    are structs, not classes. Why am I allowed to assign a value to a member of an unassigned
    Vector2
    variable, but not to a member of an unassigned
    Rect
    variable?

    Code (CSharp):
    1. public class Clazz
    2. {
    3.     public void A()
    4.     {
    5.         Vector2 v;
    6.  
    7.         v.x = 1;
    8.  
    9.         Rect r;
    10.  
    11.         r.x = 1;
    12.     }
    13. }
     
  2. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    Ah, because
    x
    is a field of
    Vector2
    , but it is a property of
    Rect
    .
     
    trisogene likes this.