プログラミング初心者です。
unityでボタンUIをスクリプトから生成しようとしています。
まずhierarchy viewでボタンオブジェクト(ButtonA)を作成し、prefab化しました。ButtonA作成と同時にcanvasが自動生成されています。
オブジェクト同様にスクリプトでボタンを生成しようとするも、生成されず。
調べるとUIはcanvasを親に指定しないとだめと知り、
C#
1objButton.transform.SetParent(canvas.transform, false);
のようにしてみました。調べる範囲内だとこのような方法で動作するはずですが、うまくいきません。何か根本的に間違えているのでしょうか。
教えていただけると幸いです宜しくお願いいたします。
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class NewBehaviourScript : MonoBehaviour 7{ 8 9 public GameObject canvas; 10 public GameObject text; 11 // Start is called before 12 // the first frame update 13 void Start() 14 { 15 GameObject objButton = (GameObject)Resources.Load("ButtonA"); 16 objButton.transform.SetParent(canvas.transform, false); 17 Instantiate(objButton, new Vector3(5.0f, 5.0f, 0.0f), Quaternion.identity); 18 } 19 20 // Update is called once per frame 21 void Update() 22 { 23 24 } 25} 26

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/01/24 11:50