Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Vector2 Setting To Zero

Discussion in 'Editor & General Support' started by the_terrible, Apr 23, 2019.

  1. the_terrible

    the_terrible

    Joined:
    Nov 4, 2015
    Posts:
    40
    I'm confused, can someone explain to me why my two variables "offsetMin0" and "offsetMax0" are changing their values to zero after Awake()?
    Code (csharp):
    1.  
    2. public class HUDChambered : MonoBehaviour
    3. {
    4.     private Vector2 offsetMin0;
    5.     private Vector2 offsetMax0;
    6.     RectTransform dist;
    7.     void Awake()
    8.     {
    9.         dist = GetComponent<RectTransform>();
    10.         Vector2 offsetMin0 = dist.offsetMin;
    11.         Vector2 offsetMax0 = dist.offsetMax;
    12.         Debug.Log(dist.offsetMin);
    13.         Debug.Log(dist.offsetMax);
    14.         Debug.Log(offsetMin0);
    15.         Debug.Log(offsetMax0);
    16.     }
    17.     void OnEnable()
    18.     {
    19.         Debug.Log(offsetMin0);
    20.         Debug.Log(offsetMax0);
    21.     }
    22. }
     

    Attached Files:

    • ss.png
      ss.png
      File size:
      4.6 KB
      Views:
      482
  2. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,727
    You have declared new vectors in Awake with the same names as the vectors defined in the class. You are not assigning to the vectors in the class, but rather to the vectors in the function.
    (You should have gotten a compiler warning alerting you that you may have made a programming mistake).
     
    the_terrible likes this.
  3. the_terrible

    the_terrible

    Joined:
    Nov 4, 2015
    Posts:
    40
    Ah, silly mistake. Thanks.
     
    ArachnidAnimal likes this.