- Home /
SetParent does not work.
Hi,
I'm trying to drag object in the bottle. I'm trying to do with collision. I want to make bottle to parent of object. But it doesn't work whatever I tried. The collider works properly. I can get debug log when the object collides with bottle.
Here is my code sample:
void Update()
{
if(isInGlass)
{
gameObject.transform.parent = Glass.transform;
}
}
private void OnTriggerEnter(Collider other)
{
Debug.Log(other.gameObject.name);
if(other.gameObject.CompareTag("Glass1"))
{
Glass = other.gameObject;
isInGlass = true;
}
}
Answer by logicandchaos · Jul 12, 2020 at 09:51 PM
try this, take out update, you don't need to keep parenting it once it's parented:
private void OnTriggerEnter(Collider other)
{
Debug.Log(other.gameObject.name);
if(other.gameObject.CompareTag("Glass1"))
{
Debug.Log("tag equals Glass1");
gameObject.transform.parent = other.gameObject;
Debug.Log(gameObject.transform.parent.name);
}
}
you said you got ot the debug, but you didn't have one inside your next condition, make sure the tag name is correct, above you check it's name but not it's tag.
Your answer
Follow this Question
Related Questions
Snaping/Merging game objects together 0 Answers
Check collision of childs Trigger in Parents script [JS] 1 Answer
How can I animate a GameObject that isn't parented, but will need to be for the animation? 1 Answer
Silly Instantiation Issue 0 Answers
Changing the parent of an object gets rid of its parents? 1 Answer