小编The*_*mer的帖子

为什么在非MonoBehaviour类中进行多个构造函数调用?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Room : MonoBehaviour {

    public ClassB classBTestInstance = new ClassB(3);

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }
}

public class ClassB {
    public ClassB(int testNum) {
        Debug.Log("hello " + testNum);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是输出:

在此输入图像描述

你可以看到它说"Hello 3"两次.我希望了解为什么会出现这种情况?我没看到我把它叫了两次.在image2中,您将看到只有一个房间实例(当我将房间类C#脚本附加到主摄像机时).我没有将此脚本附加到其他任何内容上.

在此输入图像描述

c# unity-game-engine

2
推荐指数
1
解决办法
336
查看次数

标签 统计

c# ×1

unity-game-engine ×1