CS1579 - foreach statement does not work with variables of type 'GameSession' because 'GameSession' does not contain a public instance or extension definition for 'GetEnumerator'.

in method foreach show up mistake CS1579 - foreach statement does not work with variables of type ‘GameSession’ because ‘GameSession’ does not contain a public instance or extension definition for ‘GetEnumerator’. How can change code? help pls

I dont know english well, sorry:/

using UnityEngine;

namespace Scripts.Model { public class GameSession : MonoBehaviour { [SerializeField] private PlayerData _data; public PlayerData Data => _data;

 private void Awake()
 {
     if (IsSessionExit())
     {
         DestroyImmediate(gameObject);
     }
     else
     {
         DontDestroyOnLoad(this);
     }
 }
 private bool IsSessionExit()
 {
    var sessions = FindObjectOfType<GameSession>();
      foreach(var gameSessions  in sessions)
     {
         if (gameSessions != this)
             return true;
     }
     return false;
 }

}

You are trying to iterate on a single object. You do not get an array/list back for FindObjectOfType. Maybe you are looking for FindObjectsOfType? In that case you would get an array back and you can iterate over that.