First, thank you for taking the time to read this. I am new to Unity, and I am trying to build a puzzle game. I’ve got the Level and Player being built, and the unique player motion is working, and the whole project is close to a playable Alpha thanks to Unity.
My problem is that after the Level puts in an object for the Player to pick up and move, when the Player picks it up, instead of going with the Player, the Player seems to get a copy and the Level item stays with the Level. Both the Level and the Player use GameObject arrays, using [ (Y*width)+X ] indexing for the position of the object. The Level also uses an int grid that is used to track contents.
To take the item from the Level, the Player calls this TakeItem:
public GameObject TakeItem(int X, int Y) {
grid [(Y * GridWidth) + X] = 0;
GameObject ret = Instantiate(levelItems [(Y * GridWidth) + X]) as GameObject;
Destroy (levelItems [(Y * GridWidth) + X]);
return ret;
}
But this just makes the object disappear, and the Player has nothing. Without Destroy, the player has a copy, but the Level still has the object.
I also tried:
public GameObject TakeItem(int X, int Y) {
grid [(Y * GridWidth) + X] = 0;
GameObject ret = levelItems [(Y * GridWidth) + X];
levelItems [(Y * GridWidth) + X] = null;
return ret;
}
…but I can’t seem to get the GameObject to go with the player, AND NOT stay on the Level. Either it is in both places, or it is gone completely. I’m confused.