LoginSignup
2

More than 5 years have passed since last update.

[Unityで学ぶC#] 1 変数の使用

Last updated at Posted at 2017-01-12

変数は,数学で出てくるx,yのようなものです.

ここでは,概念の説明は避け,使い方のみの説明になります.

演習1 変数

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Example1 : MonoBehaviour {

    // Use this for initialization
    void Start () {
        // 変数を宣言する
        int hp; // 1つ目の変数を宣言する.
        int damage; // 2つ目の変数を宣言する.

        // hpへ10を割りあてる
        hp = 10; // hpに10を代入

        Debug.Log ("現在のHP" + hp);

        damage = 3;
        Debug.Log ("ダメージ" + damage);

        hp -= damage; // hp から damageを引き算する
        Debug.Log ("現在のHP" + hp);
    }
}

スクリーンショット 2017-01-12 13.36.07.png

今回のスクリプトについて

変数の宣言の仕方

 変数名;

int は変数が整数であることを示します.

= の意味

hp = 10;

=は,「右のものを左につっこむ」と考えていただいて大丈夫です.

数学の「右と左が同じ」という考えとは全く異なりますね.

-= の意味

左から右を引き算します.

Debug.Logの()の中で足し算

Debug.log("現在のHP" + hp);

ここでは()ないで
"現在のHP"とhpを+で繋げています.
+ を使うことで,文字列と変数をひとまとめにできます.

参考資料 (もっと詳しく知りたい方)

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2