0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Unity】否定演算子の使い方

Last updated at Posted at 2020-04-26

環境

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じゃないよ");//コンソールに表示
        }       
    }
}

実行結果
image.png

 
・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);        
    }
}

実行結果
image.png

おわりに

なにかと使える否定演算子

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?