Search Unity

I can't create RectTransform in Awake when the GameObject is inactive at first.

Discussion in 'Scripting' started by watsonsong, Mar 15, 2017.

  1. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    I am using Unity 5.5.2p2, and I am create a RectTransform in the Awake like this:
    Code (CSharp):
    1.  
    2. void Awake()
    3. {
    4.     var go = new GameObject("New", typeof(RectTransform));
    5.     go.transform.SetParent(this.transform);
    6. }
    7.  
    And if the gameobject is inactive, and I active it by other logic or manual active it in inspector when unity is playing, it appear the error:
    This change break most of my logic, and it seems introduce recently. In 5.5.1 I never met this problem.
     
  2. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Have you tried the templated (generic) form:

    Code (csharp):
    1.          var rt = new GameObject ("New").AddComponent<RectTransform> ();
     
    forestrf likes this.
  4. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    No,I test it but the result is still the same:
    I test the newst version 5.5.2p3, the problem is still the same. But the 5.5.2p1 is fine.
     
  5. paradizIsCool

    paradizIsCool

    Joined:
    Jul 10, 2014
    Posts:
    178
    We have this issue with 5.5.3 too
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    From the documentation on GameObject constructors
    https://docs.unity3d.com/ScriptReference/GameObject-ctor.html

    Since a RectTransform is a Transform and a GameObject can only have a single Transform on it attempting to add one via code fails (as you've seen). Since the constructors have been around way longer than RectTransform and the new UI system I would imagine that this is an overlooked feature. I'd suggest creating a feature request to be able to create GameObjects with RectTransform components via code
     
  7. paradizIsCool

    paradizIsCool

    Joined:
    Jul 10, 2014
    Posts:
    178
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    If you check here:

    http://answers.unity3d.com/questions/948887/what-is-the-correct-syntax-to-create-a-new-object.html

    You can see it is possible with just this codelet:

    Code (csharp):
    1. GameObject myGO = new GameObject("MyGO", typeof(RectTransform));
    I just tried it, it works as expected: I now have a new GameObject with a RectTransform instead of a Transform.

    That said, it is probably better to play in the Prefab context, because then when you hire an artist or designer who doesn't know how to write code, they can use the Unity engine to change your prefab and you don't have to touch your code.
     
    stefan-velnita, MafiaMoe and KelsoMRK like this.
  9. paradizIsCool

    paradizIsCool

    Joined:
    Jul 10, 2014
    Posts:
    178
    Take a look at the first post, this raise an error with some unity versions. BTW documentation is often more reliable than forums.