Spy-Shifty / BrokenBricksECS

Rebuild of Unity3D upcoming Entity Component System !OUT OF DEVELOPMENT!
MIT License
162 stars 28 forks source link

Adding a component when in player mode throws an exception #20

Closed Gwindalmir closed 6 years ago

Gwindalmir commented 6 years ago

I'm trying to add a component at runtime, and it works when in Unity editor, but in a standalone player, I get an exception.

The code simply detects when the user clicks a unit, and adds a component to indicate selection:

GameObject hitObject = WorkManager.FindHitObject(Input.mousePosition);
Vector3 hitPoint = WorkManager.FindHitPoint(Input.mousePosition);
if (hitObject && hitPoint != ResourceManager.InvalidPosition)
{
    var selectedgroup = entityManager.GetComponentGroup(typeof(SelectedComponent), typeof(SelectableComponent));
    var selectedEntities = selectedgroup.GetComponent<SelectedComponent>();

    // Remove previous selections
    for (var i = 0; i < selectedEntities.Length; i++)
        entityManager.RemoveComponent<SelectedComponent>(selectedEntities.GetEntity(i));

    if (hitObject.name != "Ground")
    {
        // Select new entity
        var worldObject = WorkManager.FindComponentInObject<GameObjectEntity>(hitObject);

        if (worldObject.EntityManager.HasComponent<SelectableComponent>(worldObject.Entity))
            worldObject.EntityManager.AddComponent(worldObject.Entity, new SelectedComponent() { gameObject = worldObject.gameObject });
    }
}

// ---- Component in question
namespace Components
{
    // structs are way faster than class 
    // use this when ever you can it has an important impact on the computation speedup
    public struct SelectedComponent : IComponent
    {
        [Obsolete("Only for legacy code")]
        public GameObject gameObject;
    }    
}

In a windows standalone player, I get the following exception:

Object reference not set to an instance of an object at ECS.GameObjectEntity.OnComponentAddedToEntity[SelectedComponent] (System.Object sender, Entity entity, SelectedComponent component) [0x0008b] in C:\Users\username\Documents\game\Assets\3rdParty\ECS\Unity\GameObjectEntity.cs:105 at ECS.ComponentAddedToEntityEvent.CallEvent[SelectedComponent] (System.Object sender, ECS.Entity& entity, Components.SelectedComponent& component) [0x00072] in C:\Users\username\Documents\game\Assets\3rdParty\ECS\Core\ECSEvent.cs:189 at ECS.EntityManager.OnComponentAddedToEntity[SelectedComponent] (System.Object sender, Entity entity, SelectedComponent component) [0x0000c] in C:\Users\username\Documents\game\Assets\3rdParty\ECS\Core\EntityManager.cs:203 at ECS.ComponentAddedToEntityEvent.CallEvent[SelectedComponent] (System.Object sender, ECS.Entity& entity, Components.SelectedComponent& component) [0x00022] in C:\Users\username\Documents\game\Assets\3rdParty\ECS\Core\ECSEvent.cs:183 at ECS.ComponentArray`1[Components.SelectedComponent].Add (Entity entity, SelectedComponent component) [0x000d0] in C:\Users\username\Documents\game\Assets\3rdParty\ECS\Core\ComponentArray.cs:213 at ECS.EntityManager.AddComponent[SelectedComponent] (Entity entity, SelectedComponent component) [0x0005f] in C:\Users\username\Documents\game\Assets\3rdParty\ECS\Core\EntityManager.cs:279 at GameSystems.InputSystem.SelectUnit () [0x00139] in C:\Users\username\Documents\game\Assets\GameSystems\InputSystem.cs:90

In GameObjectEntity.cs, line 105, componentWrapperType is null. (In !UNITY_EDITOR preprocessed section).

Spy-Shifty commented 6 years ago

Fixed 0b4bfc5c44b9ec4b21c56ae3b15587402623f8f3 Thank you for reporting!