Unity 3D沙盒游戏问题

Unity 3D Sandbox Game Problems

所以我正在尝试添加在世界中放置不同块(预制件)的能力,但我一直在出错,而且我很挣扎。这是库存代码:

public bool displayInventory;

public Behaviour PlayerController;

public int currentPrefabId;

public GameObject playerInv;

public Transform playerTransform;

public Vector3 playerPosition;

void Start () {
    displayInventory = false;
    playerPosition = playerTransform.position;
}

void FixedUpdate() {

    playerPosition = playerTransform.position;

    if (Input.GetButtonDown("Open Inventory"))
    {
        displayInventory = true;

    }

    if (Input.GetButtonDown("Cancel"))
    {
        displayInventory = false;
    }

    if (displayInventory == true)
    {
        showInventory(playerInv);
        Cursor.lockState = CursorLockMode.None;
        Cursor.visible = true;
    }

    if (displayInventory == false)
    {
        closeInventory(playerInv);
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    if (Input.GetButton("Fire1"))
    {
        Sign.placePrefabSign();
    }

    if (Input.GetButton("Fire2"))
    {
        Wood.placePrefabWood();
    }
}

public static void showInventory(GameObject playerInv)
{
    playerInv.SetActive(true);  
}

public static void closeInventory(GameObject playerInv)
{
    playerInv.SetActive(false);
}

public static void PlacePrefab(int currentPrefabId, GameObject[] Prefabs, Vector3 playerPosition)
{
    Instantiate(Prefabs[currentPrefabId], playerPosition, new Quaternion(0, 0, 0, 0));
}

谁能帮我解决这个问题? (零件仍然是我试过的旧系统)。

我收到这些错误:

我已经上传了源代码,它在这里:

Source Code Download

更新:

现在我明白你想做什么了。您不需要 Wood.cs 脚本来在按下按钮时实例化预制件。因此,将 inventory.cs 的代码更改为:

using UnityEngine;

public class inventory : MonoBehaviour {

    public bool displayInventory;

    public Behaviour PlayerController;

    public int currentPrefabId;

    public GameObject playerInv;

    public Transform playerTransform;

    public Vector3 playerPosition;

    public GameObject m_oPlayer // <- NEW! : Must be linked in the editor with the player (RigidBodyFPSController);

    public GameObject m_oWoodPrefab; // <- NEW! :Must be linked in the editor with the prefab

    void Start () {
        displayInventory = false;
        playerPosition = playerTransform.position;
    }

    void FixedUpdate() {

        playerPosition = playerTransform.position;

        if (Input.GetButtonDown("Open Inventory"))
        {
            displayInventory = true;

        }

        if (Input.GetButtonDown("Cancel"))
        {
            displayInventory = false;
        }

        if (displayInventory == true)
        {
            showInventory(playerInv);
            Cursor.lockState = CursorLockMode.None;
            Cursor.visible = true;
        }

        if (displayInventory == false)
        {
            closeInventory(playerInv);
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;
        }

        if (Input.GetButton("Fire1"))
        {
            Sign.placePrefabSign();
        }

        if (Input.GetButtonDown("Fire2")) // <- CHANGED
        {
            Instantiate(m_oWoodPrefab, playerPosition, m_oPlayer.transform.rotation);
        }
    }

    public static void showInventory(GameObject playerInv)
    {
        playerInv.SetActive(true);  
    }

    public static void closeInventory(GameObject playerInv)
    {
        playerInv.SetActive(false);
    }

    public static void PlacePrefab(int currentPrefabId, GameObject[] Prefabs, Vector3 playerPosition)
    {
        Instantiate(Prefabs[currentPrefabId], playerPosition, new Quaternion(0, 0, 0, 0));
    }

}

您可以删除不需要的 wood.cs 脚本。并检查您的代码,因为您必须避免使用 "new" 关键字来创建 MonoBehavior 对象,以及静态函数。我也没有更改符号功能,但你应该...

原答案:

好的,我查看了源代码

您将脚本 class 的构造函数用作关联游戏对象的 "Initialization"。这不是正确的方法。

出现错误是因为游戏对象的引用在构造函数时不可用。因此返回 null,Instantiate 函数崩溃。

Unity3D提供的标准函数,例如

void Start ()

Start is called on the frame when a script is enabled just before any of the Update methods is called the first time.

Like the Awake function, Start is called exactly once in the lifetime of the script. However, Awake is called when the script object is initialised, regardless of whether or not the script is enabled. Start may not be called on the same frame as Awake if the script is not enabled at initialisation time.

http://docs.unity3d.com/ScriptReference/MonoBehaviour.Start.html

此外,您的代码还有其他几个问题...我建议您观看一些统一教程。

所以,这是来自 wood.cs 脚本的有效代码(我刚试过):

using UnityEngine;
using System.Collections;

public class Wood : MonoBehaviour {

    public GameObject prefab;

    public Transform playerTransfom;

    public Quaternion rotation;

    void Start() {
        Instantiate(prefab, playerTransfom.position, rotation);
    }

}