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).
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:
In a windows standalone player, I get the following exception:
In GameObjectEntity.cs, line 105, componentWrapperType is null. (In !UNITY_EDITOR preprocessed section).