C# Unity 3D沙盒游戏问题
所以我试图增加在世界上放置不同块(预制件)的能力,我只是不断地出错,我正在努力。以下是库存的代码:C# Unity 3D沙盒游戏问题,c#,unity3d,sandbox,unity5,C#,Unity3d,Sandbox,Unity5,所以我试图增加在世界上放置不同块(预制件)的能力,我只是不断地出错,我正在努力。以下是库存的代码: public bool displayInventory; public Behaviour PlayerController; public int currentPrefabId; public GameObject playerInv; public Transform playerTransform; public Vector3 playerPosition; void St
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));
}
有人能帮我吗?(部分仍然是我试用过的旧系统的一部分)
我发现以下错误:
我已经上传了源代码,它在这里:
更新: 现在我明白你想做什么了。您不需要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));
}
}
在任何脚本之前启用脚本时,在帧上调用Start
第一次调用更新方法中的一个
与唤醒函数一样,Start在生命周期中只调用一次
这是剧本的一部分。但是,当脚本对象为
初始化,无论脚本是否启用。开始
如果脚本未激活,则不能在与唤醒相同的帧上调用
在初始化时启用
此外,您的代码还有其他几个问题。。。我建议你看一些unity教程
因此,这是wood.cs脚本中的代码(我刚刚尝试过):
更新:
现在我明白你想做什么了。您不需要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));
}
}
在任何脚本之前启用脚本时,在帧上调用Start
第一次调用更新方法中的一个
与唤醒函数一样,Start在生命周期中只调用一次
这是剧本的一部分。但是,当脚本对象为
初始化,无论脚本是否启用。开始
如果脚本未激活,则不能在与唤醒相同的帧上调用
在初始化时启用
此外,您的代码还有其他几个问题。。。我建议你看一些unity教程
因此,这是wood.cs脚本中的代码(我刚刚尝试过):
您遇到了什么错误?请编辑这篇文章,并说明您遇到的错误。@TimeHopper您显然试图访问和使用一个空对象,这会导致NullReferenceException
。逐行调试以检测该对象。请参阅第14行的woods.cs。。。什么是空引用的对象?检查是否已初始化所有变量/对象@Simonepessoto它是一个名为wood的试块的预制件!如果您想要整个项目的源代码,请在此处查看您得到了什么错误?请编辑此文章,并显示您得到的错误。@TimeHopper您显然试图访问和使用一个空对象,这会导致NullReferenceException
。逐行调试以检测该对象。请参阅第14行的woods.cs。。。什么是空引用的对象?检查是否已初始化所有变量/对象@Simonepessoto它是一个名为wood的试块的预制件!如果您想要整个项目的源代码,我90%确定您不应该调用new Wood()
@Scott Chamberlain好的,那么我应该调用什么?@Simon pessoto那么您的意思是该代码有效吗?所以我应该用它,如果是的话,谢谢!此外,我已经寻找了一个教程,但没有任何可用的制作三维沙盒游戏@是的,代码是有效的(我试过了),您还应该删除placeprefablewood()函数,正如scott chamberlain告诉我们的那样。@scott chamberlain是的,当然。我忘了检查所有代码。应该删除该静态函数。(我将编辑答案)我90%确定你不应该调用new Wood()
@Scott Chamberlain好的,那么我应该调用什么?@Simon pessoto那么你的意思是该代码有效吗?所以我应该用它,如果是的话,谢谢!此外,我已经寻找了一个教程,但没有任何可用的制作三维沙盒游戏@是的,代码是有效的(我试过了),您还应该删除placeprefablewood()函数,正如scott chamberlain告诉我们的那样。@scott chamberlain是的,当然。我忘了检查所有代码。应该删除该静态函数。(我将编辑答案)
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);
}
}