---
title: "IF 语句"
content_type: tutorial
url: "https://learn.unity.com/tutorial/if-yu-ju?language=zh-cn"
language: zh-cn
difficulty: beginner
duration_minutes: 5
unity_version:
  - "2019.3"
  - "2019.2"
  - "2019.1"
  - "2018.4"
  - "2018.3"
  - "2018.2"
  - "2018.1"
  - "2017.4"
  - "2017.3"
  - "2017.2"
  - "2017.1"
  - "5.x"
  - "4.x"
supported_unity_versions:
  - name: "2019.3"
    url: "https://learn.unity.com/tutorial/if-yu-ju.md?language=zh-cn&version=2019.3"
  - name: "2019.2"
    url: "https://learn.unity.com/tutorial/if-yu-ju.md?language=zh-cn&version=2019.2"
  - name: "2019.1"
    url: "https://learn.unity.com/tutorial/if-yu-ju.md?language=zh-cn&version=2019.1"
  - name: "2018.4"
    url: "https://learn.unity.com/tutorial/if-yu-ju.md?language=zh-cn&version=2018.4"
  - name: "2018.3"
    url: "https://learn.unity.com/tutorial/if-yu-ju.md?language=zh-cn&version=2018.3"
  - name: "2018.2"
    url: "https://learn.unity.com/tutorial/if-yu-ju.md?language=zh-cn&version=2018.2"
  - name: "2018.1"
    url: "https://learn.unity.com/tutorial/if-yu-ju.md?language=zh-cn&version=2018.1"
  - name: "2017.4"
    url: "https://learn.unity.com/tutorial/if-yu-ju.md?language=zh-cn&version=2017.4"
  - name: "2017.3"
    url: "https://learn.unity.com/tutorial/if-yu-ju.md?language=zh-cn&version=2017.3"
  - name: "2017.2"
    url: "https://learn.unity.com/tutorial/if-yu-ju.md?language=zh-cn&version=2017.2"
  - name: "2017.1"
    url: "https://learn.unity.com/tutorial/if-yu-ju.md?language=zh-cn&version=2017.1"
  - name: "5.x"
    url: "https://learn.unity.com/tutorial/if-yu-ju.md?language=zh-cn&version=5.x"
  - name: "4.x"
    url: "https://learn.unity.com/tutorial/if-yu-ju.md?language=zh-cn&version=4.x"
available_languages:
  - name: "en"
    url: "https://learn.unity.com/tutorial/if-statements.md?language=en"
  - name: "es"
    url: "https://learn.unity.com/tutorial/if-statements-1.md?language=es"
  - name: "ja"
    url: "https://learn.unity.com/tutorial/if-sutetomento.md?language=ja"
  - name: "ko"
    url: "https://learn.unity.com/tutorial/if-mun-v3.md?language=ko"
  - name: "zh-cn"
    url: "https://learn.unity.com/tutorial/if-yu-ju.md?language=zh-cn"
last_updated: "2026-08-04T14:23:18.000Z"
---

# IF 语句

学习如何使用 C# 中的 IF 语句，根据条件控制代码执行流程。

如何使用 IF 语句在代码中设置条件。

本教程包含在“[初级编程](https://learn.unity.com/project/chu-ji-bian-cheng?language=zh)”项目中。

上一教程：[约定和语法](https://learn.unity.com/tutorial/yue-ding-he-yu-fa?language=zh&projectId=5e3afd88edbc2a2998252c77)

下一教程：[循环](https://learn.unity.com/tutorial/xun-huan?language=zh&projectId=5e3afd88edbc2a2998252c77)

## 1. IF 语句

[Video](https://connect-mediagw.unity.com/h1/20200206/videos/3f3ae535-8094-40e8-a87e-b39d28e42cc1_04_C__If_Statements_in_Unity!___Beginner_Scripting_Tutorial.mp4) ([Transcript (zh_cn)](https://connect-mediagw.unity.com/h1/20200206/d5cfa034-ee90-4624-bed5-c5737e6ac222_04_C__If_Statements_in_Unity____Beginner_Scripting_Tutorial___Chinese.vtt))

```csharp
using UnityEngine;
using System.Collections;

public class IfStatements : MonoBehaviour
{
    float coffeeTemperature = 85.0f;
    float hotLimitTemperature = 70.0f;
    float coldLimitTemperature = 40.0f;
    

    void Update ()
    {
        if(Input.GetKeyDown(KeyCode.Space))
            TemperatureTest();
        
        coffeeTemperature -= Time.deltaTime * 5f;
    }
    
    
    void TemperatureTest ()
    {
        // 如果咖啡的温度高于最热的饮用温度...
        if(coffeeTemperature > hotLimitTemperature)
        {
            // ... 执行此操作。
            print("Coffee is too hot.");
        }
        // 如果不是，但咖啡的温度低于最冷的饮用温度...
        else if(coffeeTemperature < coldLimitTemperature)
        {
            // ... 执行此操作。
            print("Coffee is too cold.");
        }
        // 如果两者都不是，则...
        else
        {
            // ... 执行此操作。
            print("Coffee is just right.");
        }
    }
}
```
