[Solution Found,Solved]encountered a list problem using contains

so previously i have been using Contains to find if a item existed in a list. however when i use it now it wont find the item even through the item exist?=

!(http://


for some reason it doesnt find it)

Easy, the problem is you haven’t posted any code. Use code tags when you do.

Doesn’t the debug already tell you that it doesn’t equal TypeOfSpell?

What is TypeOfSpell?

The first Debug is false, which means its an equality comparison (reference !=) issue.

TypeOfspell is as the name suggest what spell… is it and in this cause its a FireBall. but for some reason it wont find the item in the list even through it clearly says it has the spell FireBall

You are asking it to compare if they are Equal. What is TypeOfSpell, exactly? Is it even possible for TypeOfSpell to be exactly what item is?

like i told u its a Spell. of what i cast. and i feel like you are not reading what i am saying. i explained everything on the text + images.

again typeofspell is the Spell i am using. aka fireball = fireball and so on. and again it worked previously

It greatly depends on your architecture here and what TypeOfSpell is - it’s type, literally what it is.

For instance, if you have a structure like this then it’s easy to compare the class type.

using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public abstract class TypeOfSpell
{
    public string SpellName;
}

public class FireBall : TypeOfSpell
{
    public FireBall()
    {
        base.SpellName = "FireBall";
    }
}

public class SpellBook
{
    public List<TypeOfSpell> Spells;
}

public class Player : MonoBehaviour
{

    public SpellBook MySpellBook;

    public void Awake()
    {
        MySpellBook = new SpellBook();
        MySpellBook.Spells = new List<TypeOfSpell> { new FireBall() };
    }

    public void Start()
    {
        Debug.Log("We haz a fireball? " + MySpellBook.Spells.OfType<FireBall>().Any());
    }
}

But if not, then you need a different type of comparison. Is TypeOfSpell a value or reference type? You’re asking for an equality check, so you should understand what exactly that is.

If you could show how you set up your spell it would be much easier to find out. TypeOfSpell is in question here.

well i fixed my issue pretty much i forgot i had a old instance which could find objects based on typeofspell however now instead of going through a list with foreach i just search til i find my specific spell. aka like this

for (int i = 0; i < Player.PlayerSpellBook.Count; i++) {
           
            if (Player.PlayerSpellBook[i].SpellName != TypeOfSpell.SpellName) {
                Debug.Log ("Could not find Spell : "+ TypeOfSpell.SpellName);
            }

            if (Player.PlayerSpellBook[i].SpellName == TypeOfSpell.SpellName && TypeOfSpell.SpellReady == true) {

                TypeOfSpell.SpellOnCoolDown = true;
                TypeOfSpell.CastingSpell = true;
                TypeOfSpell.SpellReady = false;
                Debug.Log (" Spell Being Casted : " + TypeOfSpell.SpellName);
                GameManager._Instance._PlayerSpellBook.SpellBeingCast (TypeOfSpell.CastTime,TypeOfSpell);

            }
        }

thanks for trying to help me i am greatful for people wanting to assist D however i found my problem.

to give u guys a little more look into it…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BaseSpell  {


    private string _spellName;
    private SpellType _spellType;
    private bool _spellReady;
    private bool _spellOnCooldown;
    private bool _spellUsed;
    private bool _castingSpell;


    private int _spellId;
    private float _spellRange;
    private float _spellCooldown;
    private float _castTime;
    private float _projectileSpeed;
    private int _requiredLevel;
    private Vector3 _spellPosition;



    private GameObject _spellSprite;
    private Sprite _spellIcon;
    private List<BasicSpell> _warriorSpells = new List<BasicSpell> ();
    private List<BasicSpell> _thiefSpells = new List<BasicSpell> ();
    private List<BasicSpell> _mageSpells = new List<BasicSpell> ();
    private List<BasicSpell> _bowmanSpells = new List<BasicSpell> ();



    public enum SpellType
    {
        Fire,
        Ice,
        Lightning,
        Earth,
        Dark,
        Light,
        Wind,
        Water,

    }

    public string SpellName { get { return _spellName; } set {_spellName = value; } }
    public SpellType SpellTypeEnum { get { return _spellType; } set {_spellType = value; } }
    public int SpellId { get { return _spellId; } set {_spellId = value; } }
    public bool SpellOnCoolDown {get{return _spellOnCooldown;} set { _spellOnCooldown = value;}}
    public bool SpellReady {get{return _spellReady;} set { _spellReady = value;}}
    public bool SpellUsed {get{return _spellUsed;} set { _spellUsed = value;}}
    public bool CastingSpell {get{return _castingSpell;} set { _castingSpell = value; }}
    public float SpellRange { get { return _spellRange; } set {_spellRange = value; } }
    public float SpellCooldown { get { return _spellCooldown; } set {_spellCooldown = value; } }
    public float CastTime { get { return _castTime; } set {_castTime = value; } }
    public float ProjectileSpeed { get { return _projectileSpeed; } set {_projectileSpeed = value; } }
    public Vector3 SpellPosition { get { return _spellPosition; } set {_spellPosition = value; } }
    public GameObject SpellSprite { get { return _spellSprite; } set {_spellSprite = value; } }
    public Sprite SpellIcon {get { return _spellIcon;} set {_spellIcon = value;}}
    public int RequiredLevel { get { return _requiredLevel; } set {_requiredLevel = value; } }
    public List<BasicSpell> WarriorSpells { get { return _warriorSpells; } set {_warriorSpells = value; } }
    public List<BasicSpell> ThiefSpells { get { return _thiefSpells; } set {_thiefSpells = value; } }
    public List<BasicSpell> MageSpells { get { return _mageSpells; } set {_mageSpells = value; } }
    public List<BasicSpell> BowmanSpells { get { return _bowmanSpells; } set {_bowmanSpells = value; } }



}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BasicSpell : BaseSpell {

    public BasicSpell FireBall (){

        SpellName = "FireBall";
        SpellTypeEnum = BaseSpell.SpellType.Fire;
        SpellId = 0;
        SpellRange = 10f;
        ProjectileSpeed = 20f;
        SpellCooldown = 2f;
        CastTime = 1f;
        RequiredLevel = 1;
        SpellIcon = Resources.Load<Sprite> ("Sprites/Icon/s_astrostone");


        return this;
    }

    public BasicSpell FireBlast (){

        SpellName = "FireBlast";
        SpellTypeEnum = BaseSpell.SpellType.Fire;
        SpellId = 1;
        SpellRange = 20f;
        ProjectileSpeed = 15f;
        SpellCooldown = 8f;
        CastTime = 3f;
        RequiredLevel = 2;
        SpellIcon = Resources.Load<Sprite> ("Sprites/Icon/s_devilwake");


        return this;
    }

    public BasicSpell FrostBall (){

        SpellName = "FrostBall";
        SpellTypeEnum = BaseSpell.SpellType.Ice;
        SpellId = 2;
        SpellRange = 15f;
        SpellCooldown = 4f;
        CastTime = 2.5f;
        RequiredLevel = 3;
        SpellIcon = Resources.Load<Sprite> ("Sprites/Icon/s_wildblue");

        return this;
    }




}

each of these spells are put in depending on what class you are. aka for this its only going in the MageSpells list and if i change to my class mage i will then get the list of items inside them list of spells that mages has for my players spellbook.

obviously i am all Ears to find better ways to build my spells