Home > database >  Getting the coordinates of the parent object
Getting the coordinates of the parent object

Time:09-24

how can I get the coordinates of the parent object so that they address their parents separately. That is, there is a prefab "Flag", there are 3 such objects on the stage. When they click on them, they create another object instead of themselves. So what is the question. It's about getting the coordinates of the parent object from which it is created.

/I know, I wrote nonsense and porridge...

Code on request:

public GameObject CreatingBuildingDeleting_FlagObject;

public GameObject CreatingBuilding_Barrack_FlagObject;
public GameObject CreatingBuilding_Tent_FlagObject;

float yPosition = 0.3f;
float Z_index = 5;

public void Build_Barrack()
{
    Replace(CreatingBuildingDeleting_FlagObject, CreatingBuilding_Barrack_FlagObject);

}
public void Build_Tent()
{
    Instantiate(CreatingBuilding_Tent_FlagObject);
    CreatingBuilding_Tent_FlagObject.transform.position = new Vector3(CreatingBuildingDeleting_FlagObject.transform.parent.position.x, yPosition, Z_index);
    Destroy(CreatingBuildingDeleting_FlagObject);
}

void Replace(GameObject ReplaceObject1, GameObject ReplaceObject2)
{
    Instantiate(ReplaceObject2, ReplaceObject2.transform.position, Quaternion.identity);
    Destroy(ReplaceObject1);
}

This is how objects are created in the world

What do prefabs look like on the stage and what am I trying to create other objects from: This is how objects are created in the world

CodePudding user response:

Transform always has a reference to its parent transform! https://docs.unity3d.com/ScriptReference/Transform-parent.html Alternatively, you could also subtract transform.localPosition from transform.position because the local variant is relative to the parent and "position" is global.

CodePudding user response:

UPDATE

Actually now after closer looking at your code I think all you want to actually do would be

var position = new Vector3(CreatingBuildingDeleting_FlagObject.transform.position.x, yPosition, Z_index);
Instantiate(CreatingBuilding_Tent_FlagObject, position, Quaternion.idendity);
Destroy(CreatingBuildingDeleting_FlagObject);

Instances in the scene have no reference/information whatsoever about what original template they where cloned from.

-> You have to store and manage this on your own!

You could e.g. use a dedicated component on your original objects that you are going to clone

public class CloneInformation : MonoBehaviour
{
    // For storing the reference from where you have been cloned
    private CloneInformation _origin;
    // For public read-only acccess
    public CloneInformation Origin => _origin;

    // Optional you can even in the other direction also store direct children cloned from this object 
    private readonly HashSet<CloneInformation> clones = new HashSet<CloneInformation>(); 

    public void Initialize (CloneInformation origin)
    {
        _origin = origin;
        origin.clones.Add(this);
    }

    private void OnDestroy ()
    {
        if(_origin)
        {
            _origin.clones.Remove(this);
        }
    }   
}

And then whenever you clone such an object do e.g

var originInformation = objectToClone.GetComponent<CloneInformation>();
var clonedInformation = Instantiate (originalInformation);
clonedInformation.Initialize(originalInformation);

This way you can now get the origin position using e.g.

var originPosition = someCloneInformation.Origin.transform.position;

and could even buble up to the most top ancestor using

CloneInformation someClone;
var position = Vector3.zero;

CloneInformation current = someClone.Origin;
while(current)
{
    position = current.transform.position;

    current = current.Origin;
}
  • Related