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?
Use http://docs.unity3d.com/ScriptReference/Transform.SetParent.html:
“This method is the same as the parent property except that it’s possible to make the Transform keep its local orientation rather than its global orientation by setting the worldPositionStays parameter to false.”
GameObject go;
go.transform.SetParent(goWall.transform);
Don’t use:
GameObject go;
go.transform.parent = goWall.transform; // It's going to be deprecated.
About the attached object you should use an array and handle using FIFO or LIFO.
https://en.wikipedia.org/wiki/FIFO_and_LIFO_accounting
There are many ways to go about this. I’ll try to keep it simple.
To attach objects to the wall, you can parent them.
myGameObject.transform.parent = wallGameObject.transform;
To place objects into position, make a script that stores the positions and then assign the objects to them.
myGameObject.transform.localPosition = wallGameObject.GetComponent<PositionReferences>().GetNextPosition();
The position reference script can look this:
Public class PositionReferences : MonoBehaviour {
public Transform[] positions;
private int index = 0;
public Vector3 GetNextPosition()
{
Vector3 result = positions[index].localPosition;
index = index + 1;
return result;
}
}
To check if object is attached to wall:
if (myGameObject.transform.parent == wallGameObject.transform)
{
Debug.Log("object is attached to wall");
}
Read up on scripting and try to figure it out. Follow some tutorials to learn how to use Unity.
https://unity3d.com/learn/tutorials/modules/beginner/scripting
http://docs.unity3d.com/ScriptReference/
If you get stuck on a script and have a specific question, you can paste the code and ask about it.