- Home /
list check doesnt work anymore
I tried it this way but the Image doesn't go away when it's in the inventory. How can I fix this? When I pick it up it should check for the name in the list(inventory), and then if it's in, remove the Image.
[SerializeField] private Image Paper;
public string name;
void Start () {
Paper.enabled = false;
}
void Update () {
if (Input.GetKeyUp(KeyCode.E))
{
goAway()
}
}
void goAway()
{
for (int i = 0; i < Inventory.instance.items.Count; i++)
{
if (Inventory.instance.items[i].name == name)
{
//remove image
}
}
}
private void OnMouseOver()
{
Paper.enabled = true;
}
private void OnMouseExit()
{
Paper.enabled = false;
}
}
Answer by Klarzahs · Jan 10, 2019 at 02:18 PM
I think your name check is the cause of the error. You compare the two names of type String with a "==", which checks for the reference (or pointer). So, unless your names point to the same String object, you will always get "false" as a result.
This stackoverflow post describes it further.
You should always compare two Strings by using
if (Inventory.instance.items[i].name.Equals(name)){
...
}
Your answer
Follow this Question
Related Questions
Image in Button is cut (New UI) 2 Answers
compress photos on import 0 Answers
converting Textures to Images ? 1 Answer
How to use On-Screen Buttons in unity 5 {URGENT} 1 Answer
Draw a Sprite Round 1 Answer