零基础学做游戏 照着打试试

Hello World!

HelloWorld.cs
using System.Collections;
using UnityEngine;

public class HelloWorld: MonoBehaviour
{

    public string text = "Hello Game!";

}

Console 显示无任何结果
Inspector 显示结果如下


Inspector显示结果.png
using System.Collections;
using UnityEngine;

public class HelloWorld: MonoBehaviour
{

    public string text = "Hello Game!";
    public bool result = true;
    public float posY = 120.00f;
    public int member = 1;

}

Console 显示无任何结果
Inspector 显示结果如下


Inspector显示结果.png

Inspector 可修改


Inspector修改显示结果.png

错误代码:

using System.Collections;
using UnityEngine;

public class HelloWorld : MonoBehaviour
{

    public string text = Hello Game!;
    public bool result = yes;
    public float posY = 120.00;
    public int member = 1.2;

}
string

Assets/HelloWorld.cs(8,31): error CS1525: Unexpected symbol 'Game'
string为字符串类型,需加引号--""

bool

Assets/HelloWorld.cs(9,26): error CS0103: The name 'yes' does not exist in the current context
bool布尔值,范围True 或 False,默认值False

float

Assets/HelloWorld.cs(10,25): error CS0664: Literal of type double cannot be implicitly converted to type 'float'. Add suffix 'f' to create a literal of this type'
32 位单精度浮点型,范围-3.4 x 1038 到 + 3.4 x 1038,默认值0.0f

int

Assets/HelloWorld.cs(11,25): error CS0266: Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)
32 位有符号整数类型,范围-2 , 147 , 483 , 648 到 2 , 147 , 483 , 647,默认值0

完整数据类型
using System.Collections;
using UnityEngine;

public class HelloWorld : MonoBehaviour
{

    public bool boolTest;
    public byte byteTest;
    public char charTest;
    public decimal decimalTest;
    public double doubleTest;
    public float floatTest;
    public int intTest;
    public long longTest;
    public sbyte sbyteTest;
    public short shortTest;
    public uint unitTest;
    public ulong ulongTest;
    public ushort ushortTest;

    public object objectTest;
    public string stringTest;

}
Inspector修改显示结果.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容