list.contains returns false

in one script’s update function i have this

Debug.Log (XandYcontroller.gridlist.Contains (“hi”));

it always returns false even though in another script’s awake function it is saying

XandYcontroller.gridlist.Add (“hi”);

You generally should only use Awake() to initialise the value of local components and variables - it sounds here like you’re trying to access the gridlist attached to another object, which may or may not have been instantiated in the scene yet.
Try moving the Add() call to Start() (or, just instantiate the list with it already included as an element) and see if that makes a difference.

@tanoshimi
@Sergio7888
you guys do not understand what i am talking about.
This is my script

void Awake () {
XandYcontroller.gridlist.Add (“hi”);
}

then in another object’s script it asks

void Update () {
if (XandYcontroller.gridlist.Contains (“hi”)) {
Debug.log(true);
}
else {
Debug.log(false);
}
}

List test = new List();
test.Add(“hi”);
Debug.Log(test.Contains(“hi”));

True. It would be a bad comparer if it were otherwise.

So. that means something else is wrong here.
Well, floats are imprecise and bad for comparing. So if you’re putting them into a string they might be sometimes 1.9999999 and maybe sometimes 2. When you do ToString on 1.9999999 it displays 2. Try this and see:

float a = 1.9999999f;
Debug.Log(a); // 2 same as saying a.ToString()
Debug.Log(a == 1.9999999f); // True
Debug.Log(a == 2); // False

So hopefully that shows you what’s wrong.
How to fix it? Don’t use floats for comparison. Also, you’re usage of a string in a list for comparison is confusing. Why not a Vector2 instead? Vector2 equality comparer allows for some variation in the values (probably using Mathf.Approximately). But it’s a grid. Most of the time grids are integers. Even better to compare!

i think the problem is, it does not contain “hi”. every time you create a new string (like in the comparison) it’s a totally different object, has nothing to do with it’s value. If you want to know if “any item of the list contains the string hi” you probably need to iterate it and do an Equals check on the value.