Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

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:
    374
    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:
    374
  3. Kurt-Dekker

    Kurt-Dekker

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

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

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    374
    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:
    148
    We have this issue with 5.5.3 too
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,466
    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:
    148
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    4,527
    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.
     
    KelsoMRK likes this.
  9. paradizIsCool

    paradizIsCool

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