I am developing a 2D game, and I am very new to it, I searched almost every reference material and found no helping material. My game has a wall having three positions to place objects on it, at maximum. There are total 6 objects. When I click an object it attached onto the wall, one object should attach at one place at a time. When an object attached to first position then second object should place at second position and so on. The objects will disappear on mouse click.
The problem is that how can I attach objects on wall, at position1, position2, position3. Secondly, how can I know that there is an object attached on the wall?
If you have three spots on a wall to place an object, you can simply define those as variables. You could name them explicitly as “Obj1”, “Obj2”, “Obj3” or the like, or you could have “WallObjects” as an array of GameObjects for potential versatility to include more than 3 spots.
As an array of GameObjects, you can fill those array slots using whatever GameObjects you choose to. If you have 6 total objects, WallObjects[0] could be any of those six, WallObjects[1] could be any, and WallObjects[2] could be any of them as well.
The array would keep track of which is which, so if you destroy one of those objects, the array would now contain “null” in that slot. This would make it easy to know whether the slot has something in place.
// C#
// A generalization of the pieces involved
GameObject[] wallObjects = new GameObject[3];
public GameObject[] wallObjTypes; // Your pool of 6 object types to apply to the wall
public Vector2[] wallObjPos; // The position on the walls where the objects are placed
// Randomly populate a wall
void PopulateWall()
{
for(int i = 0; i < wallObjects.Length; i++)
{
// Instantiate a random selection from the 6 types to predefined locations on the wall
int randomType = Random.Range(0, wallObjTypes.Length);
AddToWall(i, wallObjTypes[randomType]);
}
}
// Add a specified object to the wall at a specified position
void AddToWall(int position, GameObject objToPlace)
{
wallObjects[position] = Instantiate(objToPlace, new Vector3(wallObjPos[position].x, wallObjPos[position].y, 0), Quaternion.identity);
}
For mouse clicks, take the position of a Raycast hitting the wall (or any other methodology for specifically-defined regions, but that’s entirely dependent on how the game’s designed) and determine which wallObjPos Vector is closest to the spot you hit (using a for loop).
Then, assign whichever object type to that slot as necessary. As @imp903 said, there’s a little bit of useful information missing regarding how you’re trying to put this together, so there’s no way to offer concrete suggestions beyond vague concepts.