(スクリプトの作成)
- 新規にC#スクリプトの作成
- 名前を「ColorChange」に変更
- 下記のコードを書いてチェック
using UnityEngine;
public class ColorChange : MonoBehaviour
{
private Color initialColor;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
initialColor = GetComponent<MeshRenderer>().material.color;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.B))
{
// 赤色に変化させる
GetComponent<MeshRenderer>().material.color = Color.red;
}
// 初期のカラーに戻す
if (Input.GetKeyDown(KeyCode.N))
{
GetComponent<MeshRenderer>().material.color = initialColor;
}
}
}
(実行確認)
- 「B」ボタンを押すと赤色に変化
- その後、「N」ボタンを押すと初期のカラーに戻れば成功です。