コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】Parent of RectTransform is being set with parent property

概要

m_image1.transform.parent = m_image2.transform;

UI の親オブジェクトをスクリプトから設定する時に
上記のような書き方をすると、

Parent of RectTransform is being set with parent property. 
Consider using the SetParent method instead, 
with the worldPositionStays argument set to false. 
This will retain local orientation and scale rather than world orientation and scale, 
which can prevent common UI scaling issues.

このような警告が表示されます

m_image1.transform.SetParent( m_image2.transform );

parent プロパティではなく SetParent 関数を使用することで警告を削除できます