Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by mutafow · Oct 09, 2017 at 09:57 PM · errorgameobjectupdatecomponentassignment

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FortisVenaliter · Oct 10, 2017 at 07:03 PM 1
Share

Where is dayCycle set?

avatar image mutafow FortisVenaliter · Oct 10, 2017 at 07:47 PM 0
Share

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!

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

165 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't reference script of parent 2 Answers

error CS1061: Are you missing an assembly reference? 2 Answers

[SOLVED] Stop counting score code error! 1 Answer

NullRefrenceException: Object refrence not set to an instance of an object. 0 Answers

How do I remove ALL components from a gameObject? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges