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

How to get RectTransform parameters of a not active object?

Discussion in 'UGUI & TextMesh Pro' started by gustos, Sep 22, 2015.

  1. gustos

    gustos

    Joined:
    Apr 21, 2013
    Posts:
    6
    I've tried to access it directly and through SerializedObject - they all contain something wrong like zeroes or something odd.
    If I do the same on activated object - then all ok (activated manually in inspector).
    Making gameObject.SetActive(true) just before
    gameObject.GetComponent<RectTransform>().something
    does not help.

    What I really need is to "copy" RectTransform from one object to another. And it doesn't work on disabled objects and i can't find the way.
     
  2. BinaryX

    BinaryX

    Joined:
    Aug 4, 2014
    Posts:
    55
    Where you want to use it, make a public variable and from the inspector drag the disabled game object into that spot.

    Code (CSharp):
    1. public GameObject Message;
    2.  
    3.  
    4. void Start()
    5. {
    6.      print( Message.GetComponent<RectTransform>().sizeDelta );
    7. }
    This printed the sizeDelta correctly when the game object was disabled.
     
  3. gustos

    gustos

    Joined:
    Apr 21, 2013
    Posts:
    6
    Problem solved.

    I've used GetComponentInParent to find RectTransform of gameobject of my component.
    and if "my" gameobject was deactivated - it returned RectTransform from some active parent.

    I thought component.GetComponentInParent is just a shortcut for component.gameObject.GetComponent :(