環境
Unity 2019.3.7f1
はじめに
否定演算子の使い方がわからなくなるのでメモ
使い方
使い方は2種類
・if文で使用
if(変数 != 数値)
{
処理内容
}
・trueとfalseを逆にする
変数 = !変数;
具体例
・if文で使用
using System.Collections; using System.Collections.Generic; using UnityEngine; public class test : MonoBehaviour { int a; void Start() { a = 2; //aが1ではないなら処理 if(a != 1) { Debug.Log("aは1じゃないよ");//コンソールに表示 } } }
実行結果
・trueとfalseを逆にする
using System.Collections; using System.Collections.Generic; using UnityEngine; public class test : MonoBehaviour { bool a; void Start() { a = true; a = !a; Debug.Log("a=" + a); a = !a; Debug.Log("a=" + a); a = !a; Debug.Log("a=" + a); a = !a; Debug.Log("a=" + a); } }
実行結果
おわりに
なにかと使える否定演算子