I am using the same code on two different scripts but one of my scripts is giving me a NullReferenceException
public float steel;
public float wheat;
public float meat;
public float wood;
public float leather;
public float stone;
I am accessing resources on this script and using it to build buildings and craft items
public void BuildMines()
{
if (resourceManager.wood >= 50f)
{
buildingSpecs = new Vector3(0, 2f, 0);
uiManager.BuildingButtonsInactive();
plotManager.BuildABuilding(previousSelected, mines, buildingSpecs);
resourceManager.ReduceWood(50f);
}
}
this code works when creating buildings
public void CraftHelmet()
{
steelRequirement = 20f;
leatherRequirement = 10f;
timeNeeded = 30f;
print("test");
if (resourceManager.wood >= 50f)
{
progressBar.Craft(timeNeeded);
}
}
but this gives me the error in line 7. both of my objects do find the first script and i can clearly see it in the inspector, any help would be greatly appreciated.
resourceManager is null or unassigned find out why.
ResourceManager is assigned and i can see it in the inspector. i am using the same code to find the resourcemanager in both through findobjectoftype<>. the error only occurs when trying to craft something and works perfectly fine when building
Perhaps the ResourceManager has not been created yet when you call FindObjectOfType? When and where are you calling FindObjectOfType?
More details are needed than what you have volunteered so far in this thread.
The answer is always the same… ALWAYS. It is the single most common error ever. Don’t waste your life on this problem. Instead, learn how to fix it fast… it’s EASY!!
Some notes on how to fix a NullReferenceException error in Unity3D
- also known as: Unassigned Reference Exception
- also known as: Missing Reference Exception
http://plbm.com/?p=221
The basic steps outlined above are:
- Identify what is null
- Identify why it is null
- Fix that.
Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.
This is the kind of mindset and thinking process you need to bring to this problem:
https://discussions.unity.com/t/814091/4
Step by step, break it down, find the problem.
1 Like
I was low-key expecting you.
1 Like
I am pleased by this statement.
I hope OP sorts his null out.