Which one to use for, while or something else?
Hello, I need to cycle through a array of "keys" when I try to pick up or drop a key and add or remove a key from the correct spot but I can't seem to get this to work. Code:
public void AddKey(KeyObject key)
{
for (int i = 0; i < keys.Length; i++)
{
if (keys[i] != null) break;
if (keys[i] == key) return;
if (keys[i] == null)
{
keys[i] = key;
return;
}
}
}
public void DropKey(KeyObject key)
{
for (int i = 0; i < keys.Length; i++)
{
if (keys[i] != key) break;
if (keys[i] == key)
{
keys[i] = null;
return;
}
}
}
Comment
Best Answer
Answer by Hellium · Dec 23, 2018 at 09:05 PM
Try this:
public void AddKey(KeyObject key)
{
for (int i = 0; i < keys.Length; i++)
{
if (keys[i] == key) return;
if (keys[i] == null)
{
keys[i] = key;
return;
}
}
}
public void DropKey(KeyObject key)
{
for (int i = 0; i < keys.Length; i++)
{
if (keys[i] == key)
{
keys[i] = null;
return;
}
}
}
Thank you kind sir. I'm tired and couldn't figure it out myself!