- Home /
How do I connect two objects together in the scene?
I have some buttons that are creating objects, if I click the button for another object to spawn, how do I make it attach itself to the previous object?
Okay, I have this script working with some buttons. My canvas has this script attached to it... What I want is to be able to move the next generated object to attach to the previous object. In the future I'm going to have anchor/attach points on them so that objects can only be attached to specific areas on each object. (Imagine we're building a rocket and some components only attach a specific way)... So that is the end goal eventually. Right now I want to be able to move the object and attach it to the previous object. The previous object will eventually have attach points like I said.
public class TestIntanstiation : MonoBehaviour
{
public GameObject equipPrefabCube;
public GameObject equipPrefabSphere;
//public GameObject equipPrefabetc;
public List<GameObject> createdObjects = new List<GameObject>(); // All Objects
public List<GameObject> createdCubes = new List<GameObject>(); // All Cubes
public List<GameObject> createdSpheres = new List<GameObject>(); // All Spheres
private float minX, maxX, minY, maxY;
void Start()
{
float camDistance = Vector3.Distance(transform.position, Camera.main.transform.position);
Vector2 bottomCorner = Camera.main.ViewportToWorldPoint(new Vector3(0,0, camDistance));
Vector2 topCorner = Camera.main.ViewportToWorldPoint(new Vector3(1,1, camDistance));
minX = bottomCorner.x;
maxX = topCorner.x;
minY = bottomCorner.y;
maxY = topCorner.y;
}
public void CreateObject()
{
if (equipPrefabCube != null)
{
//Vector3 position = new Vector3(Random.Range(minX + 0.5f, maxX - 0.5f), Random.Range(minY + 0.5f, maxY - 0.5f), 0);
Vector3 position = new Vector3(Random.Range(minX + 0.5f, maxX - 0.5f), Random.Range(minY + 0.5f, maxY - 0.5f), 0); // change to last pivot function
if (EventSystem.current.currentSelectedGameObject.name == "btnCube")
{
GameObject go = (GameObject)Instantiate(equipPrefabCube, position, Quaternion.identity);
createdObjects.Add(go);
createdCubes.Add(go);
}
else if(EventSystem.current.currentSelectedGameObject.name == "btnSphere")
{
GameObject go = (GameObject)Instantiate(equipPrefabSphere, position, Quaternion.identity);
createdObjects.Add(go);
createdSpheres.Add(go);
}
}
}
public void DestroyObjects()
{
foreach(GameObject obj in createdObjects)
{
Destroy(obj);
}
}
public void DestroyCubes()
{
foreach (GameObject obj in createdCubes)
{
Destroy(obj);
}
}
public void DestroySpheres()
{
foreach (GameObject obj in createdSpheres)
{
Destroy(obj);
}
}
}
This question is very vague. Please give more of a descrition, some visual aid, examples maybe and try posting what you already as a starting point.
Like Cornelis-de-Jager said. It's a little hard to know what you need. $$anonymous$$ake the second object a child of the first object with Transform.SetParent? Set a reference of the first object and use Transform.SetParent with the second object.
Answer by rufopufo · Jun 27, 2019 at 08:42 AM
Hi there,
You have to save into a variable your previous object.
When you instantiate the new object, you can just parent it to the previous object like so:
GameObject previousInstantiatedGameObject = Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity);
// ...
// Your code here
// ...
GameObject newGameObject = Instantiate(myPrefab,new Vector3(1,1,0), Quaternion.identity);
newGameObject.transform.parent = previousInstantiatedGameObject.transform;
You can set the Parent of an object by accessing their transform, and changing the value of "parent", you can also call the method "SetParent" from the transform of an object.
Hope it helps