コインの残り枚数を表示する/for文(繰り返し文)の使い方
![957ebf31 07fc 401b ad99 3b80a4df829b](https://codegenius.org/uploads/slide/image/1962/957ebf31-07fc-401b-ad99-3b80a4df829b.jpeg)
![291063e0 144d 4b03 8309 cf61a0da6cac](https://codegenius.org/uploads/slide/image/1963/291063e0-144d-4b03-8309-cf61a0da6cac.jpeg)
![E0959198 01d7 4a78 b2f8 15aa494a200e](https://codegenius.org/uploads/slide/image/1964/e0959198-01d7-4a78-b2f8-15aa494a200e.jpeg)
![45c70f37 22b4 4fb0 a649 7efac0e9b739](https://codegenius.org/uploads/slide/image/1965/45c70f37-22b4-4fb0-a649-7efac0e9b739.jpeg)
![4bc06cd5 88df 4478 8d5e 4e721b731658](https://codegenius.org/uploads/slide/image/1966/4bc06cd5-88df-4478-8d5e-4e721b731658.jpeg)
![119091a1 cc75 44a0 adb5 f38c0aa8d34f](https://codegenius.org/uploads/slide/image/1967/119091a1-cc75-44a0-adb5-f38c0aa8d34f.jpeg)
![6552a11c 4694 4b6e 9f02 abb790d0d69d](https://codegenius.org/uploads/slide/image/1968/6552a11c-4694-4b6e-9f02-abb790d0d69d.jpeg)
![34e0d0da 0948 4ff5 a413 4ded1dba9365](https://codegenius.org/uploads/slide/image/1969/34e0d0da-0948-4ff5-a413-4ded1dba9365.jpeg)
![45f9aa5b 8c77 4ecc 8a9b fa6357febba8](https://codegenius.org/uploads/slide/image/1970/45f9aa5b-8c77-4ecc-8a9b-fa6357febba8.jpeg)
![55257d81 6654 4a50 943b 55881ad68bf8](https://codegenius.org/uploads/slide/image/1971/55257d81-6654-4a50-943b-55881ad68bf8.jpeg)
![Cc671711 fb21 44a8 aeb7 1d87a942d87a](https://codegenius.org/uploads/slide/image/1972/cc671711-fb21-44a8-aeb7-1d87a942d87a.jpeg)
![Af3b263d 7250 4455 9ea2 9b8ed616ffa8](https://codegenius.org/uploads/slide/image/1973/af3b263d-7250-4455-9ea2-9b8ed616ffa8.jpeg)
![79ed58f1 2669 45ae 9711 d005417cc549](https://codegenius.org/uploads/slide/image/1974/79ed58f1-2669-45ae-9711-d005417cc549.jpeg)
![Cad933d9 351f 44f2 ae3f 00647b0c106a](https://codegenius.org/uploads/slide/image/1975/cad933d9-351f-44f2-ae3f-00647b0c106a.jpeg)
コインの残り枚数を表示する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Ball : MonoBehaviour
{
public float moveSpeed;
private Rigidbody rb;
public AudioClip coinGet;
public float jumpSpeed;
private bool isJumping = false;
private int coinCount = 0;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveH, 0, moveV);
rb.AddForce(movement * moveSpeed);
if (Input.GetButtonDown("Jump") && isJumping == false)
{
rb.velocity = Vector3.up * jumpSpeed;
isJumping = true;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(coinGet, transform.position);
coinCount += 1;
if (coinCount == 2)
{
SceneManager.LoadScene("GameClear");
}
}
}
// ★追加(コインアイコン)
public int Coin()
{
return coinCount;
}
private void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
}
![A89491ae 3e6c 418e 94b7 b45a91db48e7](https://codegenius.org/uploads/slide/image/1976/a89491ae-3e6c-418e-94b7-b45a91db48e7.jpeg)
コインの残り枚数を表示する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameController : MonoBehaviour
{
public Text timeLabel;
public float timeCount;
// ★追加(コインアイコン)
// 配列(複数のデータを入れることのできる仕切りのある箱)
public GameObject[] icons;
private Ball ballScript;
void Start()
{
timeLabel.text = "TIME;" + timeCount;
// ★追加(コインアイコン)
// 「Ball」オブジェクトに付いている「Ball」スクリプトにアクセスする。
ballScript = GameObject.Find("Ball").GetComponent<Ball>();
}
void Update()
{
timeCount -= Time.deltaTime;
timeLabel.text = "TIME;" + timeCount.ToString("0");
if (timeCount < 0)
{
SceneManager.LoadScene("GameOver");
}
// ★追加(コインアイコン)
UpdateCoin(ballScript.Coin());
}
// ★追加(コインアイコン)
// コインアイコンを表示するメソッド
void UpdateCoin(int coin)
{
// for文(繰り返し文)
for (int i = 0; i < icons.Length; i++)
{
if (coin <= i)
{
// コインアイコンを表示状態にする。
icons[i].SetActive(true);
}
else
{
// コインアイコンを非表示状態にする。
icons[i].SetActive(false);
}
}
}
}
![8b268e5b eb29 47a2 a365 caa5a18c1b5e](https://codegenius.org/uploads/slide/image/1977/8b268e5b-eb29-47a2-a365-caa5a18c1b5e.jpeg)
![01cea583 4a5e 48c2 942f b908dbc34e12](https://codegenius.org/uploads/slide/image/1978/01cea583-4a5e-48c2-942f-b908dbc34e12.jpeg)
![B4d76c54 59be 40ff a325 6be2fdbfb2f3](https://codegenius.org/uploads/slide/image/1979/b4d76c54-59be-40ff-a325-6be2fdbfb2f3.jpeg)
【2018版】BallGame(全25回)
他のコースを見る![957ebf31 07fc 401b ad99 3b80a4df829b](https://codegenius.org/uploads/slide/image/1962/957ebf31-07fc-401b-ad99-3b80a4df829b.jpeg)
![291063e0 144d 4b03 8309 cf61a0da6cac](https://codegenius.org/uploads/slide/image/1963/291063e0-144d-4b03-8309-cf61a0da6cac.jpeg)
![E0959198 01d7 4a78 b2f8 15aa494a200e](https://codegenius.org/uploads/slide/image/1964/e0959198-01d7-4a78-b2f8-15aa494a200e.jpeg)
![45c70f37 22b4 4fb0 a649 7efac0e9b739](https://codegenius.org/uploads/slide/image/1965/45c70f37-22b4-4fb0-a649-7efac0e9b739.jpeg)
![4bc06cd5 88df 4478 8d5e 4e721b731658](https://codegenius.org/uploads/slide/image/1966/4bc06cd5-88df-4478-8d5e-4e721b731658.jpeg)
![119091a1 cc75 44a0 adb5 f38c0aa8d34f](https://codegenius.org/uploads/slide/image/1967/119091a1-cc75-44a0-adb5-f38c0aa8d34f.jpeg)
![6552a11c 4694 4b6e 9f02 abb790d0d69d](https://codegenius.org/uploads/slide/image/1968/6552a11c-4694-4b6e-9f02-abb790d0d69d.jpeg)
![34e0d0da 0948 4ff5 a413 4ded1dba9365](https://codegenius.org/uploads/slide/image/1969/34e0d0da-0948-4ff5-a413-4ded1dba9365.jpeg)
![45f9aa5b 8c77 4ecc 8a9b fa6357febba8](https://codegenius.org/uploads/slide/image/1970/45f9aa5b-8c77-4ecc-8a9b-fa6357febba8.jpeg)
![55257d81 6654 4a50 943b 55881ad68bf8](https://codegenius.org/uploads/slide/image/1971/55257d81-6654-4a50-943b-55881ad68bf8.jpeg)
![Cc671711 fb21 44a8 aeb7 1d87a942d87a](https://codegenius.org/uploads/slide/image/1972/cc671711-fb21-44a8-aeb7-1d87a942d87a.jpeg)
![Af3b263d 7250 4455 9ea2 9b8ed616ffa8](https://codegenius.org/uploads/slide/image/1973/af3b263d-7250-4455-9ea2-9b8ed616ffa8.jpeg)
![79ed58f1 2669 45ae 9711 d005417cc549](https://codegenius.org/uploads/slide/image/1974/79ed58f1-2669-45ae-9711-d005417cc549.jpeg)
![Cad933d9 351f 44f2 ae3f 00647b0c106a](https://codegenius.org/uploads/slide/image/1975/cad933d9-351f-44f2-ae3f-00647b0c106a.jpeg)
コインの残り枚数を表示する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Ball : MonoBehaviour
{
public float moveSpeed;
private Rigidbody rb;
public AudioClip coinGet;
public float jumpSpeed;
private bool isJumping = false;
private int coinCount = 0;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveH, 0, moveV);
rb.AddForce(movement * moveSpeed);
if (Input.GetButtonDown("Jump") && isJumping == false)
{
rb.velocity = Vector3.up * jumpSpeed;
isJumping = true;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(coinGet, transform.position);
coinCount += 1;
if (coinCount == 2)
{
SceneManager.LoadScene("GameClear");
}
}
}
// ★追加(コインアイコン)
public int Coin()
{
return coinCount;
}
private void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
}
![A89491ae 3e6c 418e 94b7 b45a91db48e7](https://codegenius.org/uploads/slide/image/1976/a89491ae-3e6c-418e-94b7-b45a91db48e7.jpeg)
コインの残り枚数を表示する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameController : MonoBehaviour
{
public Text timeLabel;
public float timeCount;
// ★追加(コインアイコン)
// 配列(複数のデータを入れることのできる仕切りのある箱)
public GameObject[] icons;
private Ball ballScript;
void Start()
{
timeLabel.text = "TIME;" + timeCount;
// ★追加(コインアイコン)
// 「Ball」オブジェクトに付いている「Ball」スクリプトにアクセスする。
ballScript = GameObject.Find("Ball").GetComponent<Ball>();
}
void Update()
{
timeCount -= Time.deltaTime;
timeLabel.text = "TIME;" + timeCount.ToString("0");
if (timeCount < 0)
{
SceneManager.LoadScene("GameOver");
}
// ★追加(コインアイコン)
UpdateCoin(ballScript.Coin());
}
// ★追加(コインアイコン)
// コインアイコンを表示するメソッド
void UpdateCoin(int coin)
{
// for文(繰り返し文)
for (int i = 0; i < icons.Length; i++)
{
if (coin <= i)
{
// コインアイコンを表示状態にする。
icons[i].SetActive(true);
}
else
{
// コインアイコンを非表示状態にする。
icons[i].SetActive(false);
}
}
}
}
![8b268e5b eb29 47a2 a365 caa5a18c1b5e](https://codegenius.org/uploads/slide/image/1977/8b268e5b-eb29-47a2-a365-caa5a18c1b5e.jpeg)
![01cea583 4a5e 48c2 942f b908dbc34e12](https://codegenius.org/uploads/slide/image/1978/01cea583-4a5e-48c2-942f-b908dbc34e12.jpeg)
![B4d76c54 59be 40ff a325 6be2fdbfb2f3](https://codegenius.org/uploads/slide/image/1979/b4d76c54-59be-40ff-a325-6be2fdbfb2f3.jpeg)
コインの残り枚数を表示する/for文(繰り返し文)の使い方