Time Hopper
Time Hopper

Reputation: 13

Unity 3D Sandbox Game Problems

So I'm trying to add the ability to place different blocks (prefabs) in the world and I just keep getting errors and I am struggling. Here is the code for the inventory:

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

Can anyone help me with this? (Parts are still of an older system I tried out).

I am getting these errors:

enter image description here

I've uploaded the source code it is here:

Source Code Download

Upvotes: 0

Views: 491

Answers (1)

Simone Pessotto
Simone Pessotto

Reputation: 1581

UPDATE:

Now I understood what you wanted to do. You don't need a Wood.cs script to instantiate a prefab on button pressed. So change your code of inventory.cs like:

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

}

You can delete the wood.cs script you don't need it. And check your code because you have to avoid the "new" keyword to create MonoBehavior objects, also static functions. I didn't change the sign function also, but you should ...

Original Answer:

Ok, I checked the source code.

You're using the constructors for the scripts class as "Initialization" of the associate gameobject. This is not the right way.

The errors comes because the reference of the gameobject was not available at constructor time. So null was returned and the Instantiate functions crashed.

The are standard functions that Unity3D provides, for example

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

Also, there are several other problems with your code ... I suggest you to watch some of unity tutorial.

So, this is code from the wood.cs scripts that works (I just tried it):

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

}

Upvotes: 2

Related Questions