简体   繁体   English

C#代码在Unity中不起作用

[英]C# code not working in Unity

I have just started learning the language C# from the book "Learning C# by Developing Games with Unity 3d". 我刚开始从“使用Unity 3d开发游戏学习C#”一书中学习C#语言。 I have been working through the book fine up until page 47 when it gives me the following code. 我一直在完成这本书,直到第47页,它给了我以下代码。

using UnityEngine;
using System.Collections;

public class LearnScript : MonoBehaviour 
{
    public int number1 = 2;
    public int number2 = 3;
    public int number3 = 4;


    void start()
    {
        AddTwoNumbers(number1, number2);
        AddTwoNumbers(number1, number3);
        AddTwoNumbers(number2, number3);
    }

    void update()
    {

    }

    void AddTwoNumbers (int firstNumber, int secondNumber)
    {
        int result = firstNumber + secondNumber;
        Debug.Log(result);
    }
}

What the book says it is meant to do is output the answers to the AddTwoNumbers method, but when I click play on unity the console is empty. 本书所要做的就是输出AddTwoNumbers方法的答案,但当我单击“统一”时,控制台为空。

I have attached the code to the main camera so that shouldn't be a problem there. 我已将代码附加到主摄像头,因此不应该是一个问题。 If someone can tell me what I am doing wrong it would be appreciated. 如果有人能告诉我我做错了什么,我将不胜感激。 I don't want to move on with the book until I get this little bit of code to work. 在我得到一些代码工作之前,我不想继续阅读本书。 If it makes any difference I am using Unity version 5.2.3. 如果它有所不同我使用Unity版本5.2.3。

First, make sure this is attached to some object in the scene. 首先,确保将其附加到场景中的某个对象。

Second, rename: 二,重命名:

void Start()
{
    AddTwoNumbers(number1, number2);
    AddTwoNumbers(number1, number3);
    AddTwoNumbers(number2, number3);
}

Start() , not start() . Start() ,而不是start()

Also it is Update() , not update() . 它也是Update() ,而不是update()

C# is case sensitive. C#区分大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM