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 giraffe1 · Nov 16, 2016 at 05:41 AM · c#scripting problemreferencing

c# coding question

Hi,

I am trying to get each gameobject that has the entity script on it, to register itself with the entity manager script only when it is enabled.

This is my singleton entity manager script:

 using UnityEngine;
 using System.Collections.Generic;
 
 public class EntityManager : MonoBehaviour
 {
     public static EntityManager instance { get; private set; }
 
     private static uint nextId = 0;
     public static uint NextId
     {
         get { return nextId++; }
     }
 
     private Dictionary<GameObject, Entity> entityLookup;
 
     private List<Entity> agents;
     public List<Entity> Agents
     {
         get { return agents; }
     }
 
     private void Awake()
     {
         instance = this;
 
         entityLookup = new Dictionary<GameObject, Entity>();
         agents = new List<Entity>();
     }
 
     public void RegisterEntity(Entity entity)
     {
         entityLookup.Add(entity.gameObject, entity);
         agents.Add(entity);
     }
 }


And this is the entity script I put on each NPC:

 using UnityEngine;
 using PixelCrushers.LoveHate;
 
 public class Entity : MonoBehaviour
 {
     private uint id;
     public uint Id
     {
         get { return this.id; }
         set { this.id = value; }
     }
 
     public Vector3 Position
     {
         get { return this.transform.position; }
         set { this.transform.position = value; }
     }
 
     private FactionMember factionMember;
 
     public int FactionId
     {
         get { return this.factionMember.factionID; }
     }
 
     private void Awake()
     {
         factionMember = this.GetComponent<FactionMember>();
     }
 
     private void OnEnable()
     {
         this.Id = EntityManager.NextId;
         EntityManager.instance.RegisterEntity(this);
     }
 }

When I hit play I keep getting the following error:

 NullReferenceException: Object reference not set to an instance of an object
 Entity.OnEnable () (at Assets/Stuff/Scripts/Components/Entity.cs:34)

Line 34 is:

 EntityManager.instance.RegisterEntity(this);




*Edit, I have no clue what I am doing wrong.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ElijahShadbolt · Nov 16, 2016 at 08:26 AM

It could be that the Entity script's OnEnabled function is called before the EntityManager's Awake function, which is when you assign instance as the EntityManager. This means before the Awake function is called, instance will be null. You could edit the instance property with a custom get, where if the reference is null use GameObject.FindObjectOfType<EntityManager>() to find the singleton.

Comment
Add comment · Show 1 · Share
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 giraffe1 · Nov 17, 2016 at 01:53 AM 1
Share

Thanks, it turned out nothing was wrong. I just needed to close and re-open the Unity project and the errors went away. Your suggestion does sound like a good idea though!

Thanks.

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

245 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 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

videoPlayer Script Help Needed 0 Answers

Can't reference script. 1 Answer

I have 12 objects using the same script below, I need them to each activate individually of each other. 0 Answers

2D Platformer Gun Equip Script Not Working 0 Answers

Continuously monitor childrens properties 1 Answer


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