OK so this one is completely off course, and I’m not sure if Brian knows about this or not, but I’ll ask anyway. A while ago, @bixarrio helped me out to create an awesome Crafting System (thanks @bixarrio ). However, to expand on the system a little bit, I tried implementing a proper ‘HandleRaycast()’ system, which basically is supposed to open the Crafting UI when the player gets to the table. My first attempt was a sloppy one, but my second attempt, whilst it opens up the UI normally on arrival of the player when he arrives to the table, my game gives out a constant NRE when the UI is open, and basically the crafting system gets messy. Here is what my latest attempt to get this to work looks like:
- I created a new script, attached to the player, called ‘MoveToCraftingTable.cs’, as follows:
using RPG.Core;
using RPG.Movement;
using UnityEngine;
namespace RPG.Crafting {
public class MoveToCraftingTable : MonoBehaviour, IAction
{
private CraftingTable target;
private int acceptanceRadius;
private void Start() {
acceptanceRadius = 3;
}
public void Craft(CraftingTable target) {
GetComponent<ActionSchedular>().StartAction(this);
this.target = target;
}
public void Update() {
if (target == null) return;
if (!GetIsInRange(target.transform)) {
GetComponent<Mover>().MoveTo(target.transform.position, 1.0f);
}
else {
GetComponent<Mover>().Cancel();
target.NotifyCrafting();
}
}
public bool GetIsInRange(Transform target) {
return Vector3.Distance(transform.position, target.transform.position) <= acceptanceRadius;
}
public void Cancel()
{
target = null;
GetComponent<Mover>().Cancel();
}
}
}
- I went to ‘CraftingTable.cs’ and injected the new ‘IRaycastable’ Interface inheriting functions:
// Test (Delete if failed):
private MoveToCraftingTable craftingTable;
public CursorType GetCursorType()
{
// Return the 'crafting' cursor type
return CursorType.Crafting;
}
public bool HandleRaycast(PlayerController callingController) {
if (Input.GetMouseButtonDown(0)) {
craftingTable.Craft(this);
}
return true;
}
and this function as well (which is relevant to the NRE):
bool ICraftingTable.CallerIsInRange() {
// NOT THE SAME as 'distanceToCraftingTable' in 'HandleRaycast', because this one checks for 'acceptanceRadius' as well
return Vector3.Distance(transform.position, callerTransform.position) <= maxDistanceToCloseCraftingUI;
}
// and my NotifyCrafting() function:
public void NotifyCrafting() {
// This function is what is called to open the Crafting UI up
// Let the CraftingMediator know that we are being interacted with
var craftingMediator = CraftingMediator.GetCraftingMediator();
craftingMediator.NotifyInteraction(this);
}
and here is the ‘ICraftingTable.cs’ Interface components:
using System;
namespace RPG.Crafting
{
public enum CraftingState
{
Idle,
Crafting
}
public interface ICraftingTable
{
event Action<ICraftingTable> OnInteract;
event Action CraftingStarted;
event Action<float> CraftingProgress;
event Action CraftingCompleted;
event Action CraftingCancelled;
Recipe CurrentRecipe { get; }
CraftedItemSlot CraftedOutput { get; }
CraftingState CurrentState { get; }
float CraftingPercentage { get; }
Recipe[] GetRecipesList();
bool CanCraftRecipe(Recipe recipe);
void CraftRecipe(Recipe recipe);
void CancelCrafting();
void UpdateState();
bool CallerIsInRange(); // additional function by Bahaa, so that our game closes the Crafting UI if we suddenly walk away
}
}
And finally, this is the NRE I keep getting:
NullReferenceException: Object reference not set to an instance of an object
RPG.Crafting.CraftingTable.RPG.Crafting.ICraftingTable.CallerIsInRange () (at Assets/Asset Packs/bixarrio/RPG Crafting System/Scripts/CraftingTable.cs:151)
RPG.Crafting.CraftingSystem.Update () (at Assets/Asset Packs/bixarrio/RPG Crafting System/Scripts/CraftingSystem.cs:96)
Which only occurs when the player arrives to the table
How do I fix this?