Entitas.Group.GetSingleEntity C# (CSharp) Method

GetSingleEntity() public method

public GetSingleEntity ( ) : Entity
return Entity
        public Entity GetSingleEntity()
        {
            if(_singleEntityCache == null) {
                var c = _entities.Count;
                if(c == 1) {
                    using (var enumerator = _entities.GetEnumerator()) {
                        enumerator.MoveNext();
                        _singleEntityCache = enumerator.Current;
                    }
                } else if(c == 0) {
                    return null;
                } else {
                    throw new GroupSingleEntityException(this);
                }
            }

            return _singleEntityCache;
        }

Usage Example

        private void SetGameGroup(Group group)
        {
            //The group should have 1 thing, always, but...
            //FIXME: after multiple restarts (via 'r' button in HUD) this fails - srivello
            if (group.count == 1)
            {
                _gameEntity = group.GetSingleEntity();
                _gameEntity.OnComponentReplaced += Entity_OnComponentReplaced;

                //set first value
                var scoreComponent = _gameEntity.score;
                SetText (scoreComponent.score);

            }
            else
            {
                Debug.LogWarning ("CC _scoreGroup failed, should have one entity, has count: " + group.count);
            }
        }
All Usage Examples Of Entitas.Group::GetSingleEntity