UnityでボタンをクリックするたびにTextを変えたいのですが変化しません。
画面にはButtonとTextがあります。それぞれ下記のスクリプトが関連付けられています。
Buttonのスクリプトではクリックされるとプロパティのcountがインクリメント
されます。
Textのスクリプトでは上記のプロパティを参照して表示します。
しかし、Buttonをクリックしても0しか表示されません。
Textのスクリプト内でnewしているため初期化され更新されないのでしょうか?
1)ボタンのスクリプトはこちら
using System.Collections; using System.Collections.Generic; using UnityEngine; public class buttonevent : MonoBehaviour { private int count; public int Count { get{ return count; } set{ count = value; } } void Start () { count = 0; } // Update is called once per frame void Update () { } public void OnCliCK(){ //クリックするとcountが増える。 count++; Debug.Log (Count); } }
2)Textのスクリプトはこちら
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class changeText : MonoBehaviour { buttonevent button = new buttonevent(); private Text targetText; // Use this for initialization void Start () { } // Update is called once per frame void Update () { //buttoneventのCountが表示されるはずだが0しか表示されない this.targetText = this.GetComponent<Text>(); this.targetText.text = button.Count.ToString(); } }
複数のクラスにまたがるプロパティを設定すればいいのでしょうか?
その場合、それぞれのクラスでnewしてしまうと他のクラスで値を変更しても反映されないのではと思うのですが。
以上、よろしくお願いいたします。

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