- Home /
How to attach an object onto another object?
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?
Duplicate: http://answers.unity3d.com/questions/58486/how-do-i-programatically-attach-objects-to-each-ot.html (there are good answers there -- especially for physics objects).
Answer by Jessespike · Jun 18, 2015 at 08:54 PM
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.
It gives an error IndexOutOfRangeException: Array index is out of range in PositionReferences file. and another problem is that once I click an object it attached on the wall an gives the above error, but when I click another object it does not displays it on the second position on the wall. Else object attachment and check the object attached or not, is working perfectly. I can provide code if needed.
The list needs to be populated. After putting PositionReferences script on the wall, create empty GameObjects and drag them into the list, these will represent the positions to use. I didn't add any error checking, but you should add one in GetNextPosition(), something like this should work:
if (positions.Count == 0 || index >= positions.Count) {
Debug.Log("trying to use a reference that doesn't exist");
return Vector3.zero;
}
Thank you for your help. I almost got half of the solution. Can you help me more? Up till know, the objects are able to attach at their appropriate positions. Now the issues are, firstly when I move my cursor onto that object it attached onto all the positions, rather than it should attach object at only one position when I click on that specific object(I am using Physics.Raycast for object detection,is this makes a problem?). Secondly, I also have to make a check before attaching objects onto the wall that, position1 should be filled first, position2 should be filled provided position1 is not empty, and position3 should be filled provided position2 is not empty, and how can I de-attach object from wall when I click buttons some where else, other than the wall. I also have to attach and de-attach objects through out the game, this code only attach objects at all positions once then do not wait for empty position to re-attach again.
Answer by Ricna · Jun 18, 2015 at 09:35 PM
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
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Most efficient way to remove a null object from a dictionary 3 Answers
Any way to use multiple font styles within a single UI Text component? 1 Answer
Is it possible to access OnValidate() when using a custom inspector? 1 Answer