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
  4. Dismiss Notice

'RectTransform' does not contain a definition for 'drivenByObject'

Discussion in 'Scripting' started by AdamDice, Jul 28, 2022.

  1. AdamDice

    AdamDice

    Joined:
    Jun 20, 2022
    Posts:
    7
    I am creating a sheet dynamically and for that, I attached VerticalLayoutGroup object to my GameObject "Content".

    Then I create an empty GameObject Child with the RectTransform object.

    Code (CSharp):
    1. GameObject Head = new GameObject("Head2", typeof(RectTransform));
    2. RectTransform rt = Head.GetComponent<RectTransform>();
    3. rt.drivenByObject = Content.GetComponent<UnityEngine.UI.ContentSizeFitter>();
    4. rt.sizeDelta = new Vector2(1870, 100);
    5. rt.localScale = new Vector3(1.0f, 1.0f, 1.0f);
    But I have a problem. According to the documentation, https://docs.unity.cn/2021.2/Documentation/ScriptReference/RectTransform-drivenByObject.html, drivenByObject is a public property. However I cannot set drivenByObject. VSCode raise an error

    'RectTransform' does not contain a definition for 'drivenByObject' and no accessible extension method 'drivenByObject' accepting a first argument of type 'RectTransform' could be found (are you missing a using directive or an assembly reference?) [Assembly-CSharp]csharp(CS1061)
    Once again, I cannot find something related to my problem on internet...

    I decided to dirtily manage the RectTransform commenting the line with "rt.drivenByObject" and setting the localScale property. But even if there is no compilation error. The code just doesn't work in Unity

    upload_2022-7-28_10-49-4.png

    As you can see, The Scale value is (108, 108, 108) but not (1, 1, 1).

    How can I set the drivenByObject property and set the Scale property ?
     
  2. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    Hmm I've never seen drivenByObject property before, but a peek at the definion seems to show that it only has a getter. It looks to be readonly.
     
  3. AdamDice

    AdamDice

    Joined:
    Jun 20, 2022
    Posts:
    7
    What is more a problem is that even if the code

    Code (CSharp):
    1. rt.localScale = new Vector3(1.0f, 1.0f, 1.0f);
    runs. It does not work. It's just not logical. There is the property, I change the property, it works in the other scripts, the same line of code ".localScale = new Vector3(1.0f, 1.0f, 1.0f);" it use to work but here no. And I just don't have any idea why.

    It's like running a simple program "hello, world" on the console and the console prints "No, I won't print that, I will print toto".
     
  4. AdamDice

    AdamDice

    Joined:
    Jun 20, 2022
    Posts:
    7
    I have added that

    Code (CSharp):
    1. // Here, the line "Head.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);" just does not work. Don't ask why...
    2. Head.transform.localScale = Head.transform.localScale / 108;
    But it's very ugly....
     
    Last edited: Jul 28, 2022
  5. AdamDice

    AdamDice

    Joined:
    Jun 20, 2022
    Posts:
    7
    I don't understand how Unity affect value on scale. I have made that :

    Code (CSharp):
    1. Head.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
    2. //Head.transform.localScale.Scale(Head.transform.localScale);
    3. //Head.transform.localScale.Normalize();// = 1.0f;
    4. float toto = Head.transform.localScale.x;// 108.0f;
    5. Debug.Log(toto);
    6. Debug.Log(Head.transform.localScale);
    It prints that :

    upload_2022-7-28_11-57-44.png

    But in final, I have this value of localscale

    upload_2022-7-28_11-58-11.png

    I think, Unity change the value of Scale after the function is called but I don't know where it multiply by 108.04.
     
  6. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    I wonder if it's one of the parent objects, scalers or layout components in your heirarchy having an effect. I tried and failed to replicate this here.

    Maybe try disabling all the layout components and checking to see if your scale sets properly reenabling one by one to try and find the culprit.