- Home /
Moving parent from child' script doesn't work
Hi guys,
I have a weird problem with my script.
I have a GameObject with the following hierarchy:
Piece and Miniature are roughly the same in my test, a SpriteRenderer with the same image but with different size. However, in the definitive version, Piece will have a far more complicated hierarchy and I don't want to have this hierarchy upon switching to miniature, which is why I have two gameObject and not one that I resize.
I also have an area and when my "InventaireCollider" hits it, it switches from Piece to Miniature and reverse when leaving. This part works well.
However, upon switch, I want the position of "GameObject" object to be set to mouse position.
The object I move is GameObject, it has a Script called TestPiece on it with the following method:
public void switchDisplay(bool isMini)
{
Transform piece = transform.Find("Piece");
Transform miniature = transform.Find("Miniature");
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = new Vector3(mousePosition.x, mousePosition.y, transform.position.z);
piece.gameObject.SetActive(!isMini);
miniature.gameObject.SetActive(isMini);
}
"InventaireCollider" has also a script on it:
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Test")
{
TestPiece parentScript = transform.parent.transform.GetComponent<TestPiece>();
parentScript.switchDisplay(true);
}
}
void OnTriggerExit2D(Collider2D other)
{
if (other.tag == "Test")
{
TestPiece parentScript = transform.parent.transform.GetComponent<TestPiece>();
parentScript.switchDisplay(false);
}
}
My issue is the following:
When I call switchDisplay(true) from a method of the TestPiece script, for example OnMouseDown(), the method works perfectly fine, the displayed object switch from Piece to Miniature and the GameObject position is moved to the mousePosition.
However, when the method is called from the InventaireCollider script, it switches correctly from Piece to Miniature BUT "GameObject" object doesn't move and I don't know why.
Do you have any idea how to solve this problem, please?
Regards,
Kylar78