NullPointException Object not found, caused by a variable not accepting value?
Hi,
I am making a script that should spawn a prefab, add a component to it and the component should run Work() function, however I get that nullPointExcept object not attached to instance of object for the Work() function. Actually I found where the problem appears but I can not seem to understand why.
NullReferenceException: Object reference not set to an instance of an object
Waiter.Work () (at Assets/Scripts/Worker/Waiter.cs:32)
WorkerInfo.Update () (at Assets/Scripts/Worker/WorkerInfo.cs:14)
I have the WorkerInfo script:
public class WorkerInfo : MonoBehaviour
{
public Worker worker;
public WorkerInfo(Worker worker)
{
this.worker = worker;
}
private void Update ()
{
worker.Work();
}
}
Pretty basic. It is attached to the gameobject spawned and should call Work() depending on the worker type. Could be either Cook or Waiter. For now I have only implemented waiters: public class Waiter : Worker {
public int mealEveryXMinutes;
private int lastMinuteServed = 1;
private DayCycle dayCycle;
private TimeData time;
public Waiter(string name, int skill, float salary)
{
id = ++Worker.lastWorkerId;
this.name = name;
this.skill = skill;
this.salary = salary;
mealEveryXMinutes = calculateMealFreqFromSkill();
}
private void Awake()
{
dayCycle = GameObject.Find("GameManager").GetComponent<DayCycle>();
}
public override void Work()
{
if (OrderStack.orders.Count == 0) return;
time = dayCycle.getTime();
int currMin = Mathf.FloorToInt(time.minutes);
if ( currMin % mealEveryXMinutes == 0
&& lastMinuteServed != currMin)
{
lastMinuteServed = currMin;
serveFood();
}
}
private int calculateMealFreqFromSkill()
{
return (20 - Random.Range(skill / 2, skill));
}
private void serveFood()
{
Order currentOrder = OrderStack.orders[0];
currentOrder.customer.receiveFood(currentOrder.food);
OrderStack.orders.RemoveAt(0);
Debug.Log("<color=green>" + currentOrder.customer.name + " served " + currentOrder.food.FoodName + " by " + name + "</color>");
}
}
I don't think you actually need these bits of code but for the sake of solving the problem here they are. And lastly where I think the problem appears. Its on the script that should spawn the objects:
private static void createWorkerObjects()
{
foreach (Worker worker in workers)
{
Transform workerObj = Instantiate(stWorkPrefab).transform;
WorkerInfo workerComp = workerObj.gameObject.AddComponent(typeof(WorkerInfo)) as WorkerInfo;
workerComp.worker = worker;
workerComp.gameObject.name = worker.name;
workerObjects.Add(workerObj);
}
}
workerComp.gameObject.name = worker.name
works fine, so worker.name is what it has to be. The problem seems to be workerComp.worker = worker;
as everything else seems to be find? Any help would be appreciated!
Thanks in advance!
Actually as I saw your comment went back and investigated a bit more about DayCycle. Comes up I instantiate dayCycle in Awake() which is not called upon AddComponent(), therefore dayCycle var is left unassigned. Thanks you again for your comment!