Unity 3D沙盒游戏问题

时间:2016-07-17 06:47:50

标签: c# unity3d sandbox unity5

所以我试图添加在世界上放置不同块(预制件)的能力,我只是不断收到错误,我正在努力。以下是清单的代码:

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));
}

任何人都可以帮我吗? (部件仍然是我试过的旧系统)。

我收到了这些错误:

enter image description here

我已经上传了这里的源代码:

Source Code Download

1 个答案:

答案 0 :(得分:2)

<强>更新

现在我明白了你想做什么。您不需要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对象,还要创建静态函数。我也没有更改签名功能,但你应该......

原始答案:

好的,我检查了源代码。

您正在使用脚本类的构造函数作为关联游戏对象的“初始化”。这不是正确的方法。

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

这是Unity3D提供的标准功能,例如

void Start ()
  

当在任何脚本之前启用脚本时,在帧上调用Start   第一次调用Update方法。

     

与Awake功能一样,Start在生命周期中只调用一次   的脚本。但是,脚本对象时会调用Awake   无论脚本是否已启用,都会初始化。开始   如果脚本不是,则可能无法在与唤醒相同的帧上调用   在初始化时启用。

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);
    }

}