Question by
pointless_here · Mar 31, 2020 at 10:21 PM ·
scripting problemscripting beginner
Script does not detach player from moving platform, have searched forum,Why is OnTriggerExit not deparenting the target object?
Script: public class MovePlatform : MonoBehaviour {
public Transform movingPlatform;
public Transform position1;
public Transform position2;
public Vector3 newPosition;
public string currentState;
public float smooth;
public float resetTime;
// Start is called before the first frame update
void Start()
{
ChangeTarget();
}
// Update is called once per frame
void Update()
{
movingPlatform.position = Vector3.Lerp(movingPlatform.position, newPosition, smooth * Time.deltaTime);
}
void ChangeTarget()
{
if (currentState == "Moving To Position 1")
{
currentState = "Moving To Position 2";
newPosition = position2.position;
}
else if (currentState == "Moving To Position 2")
{
currentState = "Moving To Position 1";
newPosition = position1.position;
}
else if (currentState == "")
{
currentState = "Moving To Position 2";
newPosition = position2.position;
}
Invoke("ChangeTarget", resetTime);
}
public GameObject target = null;
void OnTriggerStay(Collider col)
{
target = col.gameObject;
target.transform.SetParent(transform, false);
}
void OnTriggerExit(Collider col)
{
GameObject target = null;
Debug.Log("Exit Attempted");
}
},script successfully parents the object it collides with but does not detach it upon OnTriggerExit firing public GameObject target = null;
void OnTriggerStay(Collider col)
{
target = col.gameObject;
target.transform.SetParent(transform, false);
}
void OnTriggerExit(Collider col)
{
GameObject target = null;
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612230216im_/https://answers.unity.com/themes/thub/images/avi.jpg)