前提・実現したいこと
初めて質問させていただきます。
今、2つのcubeをSceneにいているのですが、その2つのcubeの色をそれぞれクリックイベントを使用して違う色に変えたいと思っています。
質問が二つあります。
1)クリックイベント(click)を使用しており、色を白から赤色に変えるところまではできました。
ですが、もう一度元の色に戻すことができません。
Input.GetMouseButtonDownを使用して色を元に戻そうとしているのですが、間違っているのでしょうか?
2)オブジェクトをクリックすると、2つとも同じ色になってしまいます。
1つを押したら赤色になり、もう1つを押したら紫色と言うように押したオブジェクトだけが色を変えられるようにしたいです。
よろしくお願いします。
発生している問題・エラーメッセージ
特にありません。
エラーメッセージ
該当のソースコード
ソースコード using System.Collections; using System.Collections.Generic; using UnityEngine; public class EventTriggerOne : MonoBehaviour { Rigidbody rigidBody; public Vector3 force = new Vector3(0, 10, 0); public ForceMode forceMode = ForceMode.VelocityChange; public GameObject ken_do; public GameObject ken_re; // Use this for initialization void Start () { rigidBody = gameObject.GetComponent<Rigidbody>(); } public void Event() { rigidBody.AddForce(force, forceMode); } // Update is called once per frame void Update () { if (Input.GetMouseButtonDown(0)) { Debug.Log("色が変わるよ!"); //ドの音 Material ken_do = this.GetComponent<Renderer>().material; ken_do.color = new Color(1.0f, 0.0f, 0.0f, 1.0f); //レの音 Material ken_re = this.GetComponent<Renderer>().material; ken_re.color = new Color(1.0f, 0.0f, 1.0f, 1.0f); } if(Input.GetMouseButtonDown(1)) { Debug.Log("元の色に変わるよ!"); Material ken_do = this.GetComponent<Renderer>().material; ken_do.color = new Color(0.0f, 0.0f, 0.0f, 1.0f); Material ken_re = this.GetComponent<Renderer>().material; ken_re.color = new Color(0.0f, 0.0f, 1.0f, 1.0f); } } }
試したこと
補足情報(FW/ツールのバージョンなど)
unityのバージョン 2017.3.03f
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/20 10:49
退会済みユーザー
2018/10/20 10:58
2018/10/22 03:11
退会済みユーザー
2018/10/22 03:15