- Home /
How to control parenting in multiple functions
How can I control parenting and unparenting over multiple functions in one script, when I want to control the unparenting in OnCollisionExit it is not working, because it is unknown? Also the part about Parenting in the script refference is really short and does not really help.
function OnCollisionEnter(other : Collision)
{
var myTransform = other.transform;
myTransform.parent = transform;
//
if(whatEver)
{
playerTransform.parent = null;
}
}
//
function OnCollisionExit(other : Collision)
{
//Unparenting will not work because myTransform is unknown, How to make it so, that I can control it from OnEnter & OnExit??
//myTransform.parent = null;
}
Answer by Datael · May 25, 2012 at 03:26 PM
You need to declare the variable outside of the function to do that. Try replacing the first 3 lines with these:
var myTransform : Transform;
function OnCollisionEnter(other : Collision)
{
myTransform = other.transform;
//
This changes the scope of the myTransform variable from local to the OnCollisionEnter function to a class-wide scope so you can access it from anywhere in the class.
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
add relative force to child after detach.children 1 Answer
Becoming parent after OnTriggerStay/Enter? 1 Answer
How can I activate an empty object which is parent of child objects? 1 Answer
Is there a way to make a selected object a child of another object during the game? 1 Answer