Parent object to instantiate
I am making a 3d platform game and I want platforms to spawn procedurally (which is working) but when my character leaves the first platform, and runs on the next one it doesn't get parented to it. Collision detection works fine since I have another script that allows me to control the platform when collision with character is detected. Here is part of the platform script:
void OnTriggerEnter(Collider Character)
{
Character.transform.parent = this.transform;
onplatform = true;
this.transform.parent = PlatformController.transform;
}
void OnTriggerExit(Collider Character)
{
Character.transform.parent = null;
Destroy(gameObject, 2f);
this.transform.parent = null;
}
And here is the spawner code
public GameObject Platform_next;
public float timer;
public float StopTime;
public float platformLocation_y;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (timer > StopTime)
{
platformLocation_y = platformLocation_y + 14.73f;
GameObject newPlatform = Instantiate(Platform_next);
newPlatform.transform.position = transform.position = new Vector3(0f, 0f, platformLocation_y);
newPlatform.transform.localRotation = Quaternion.Euler(90, 0, 0);
timer = 0;
}
timer += Time.deltaTime;
}
}
Answer by szergopolska · Oct 12, 2020 at 02:10 PM
I solved it, I had to add OnTriggerStay where I put transform.parent and it worked.
Your answer
Follow this Question
Related Questions
DontDestroyOnLoad not working after changing parent temporary 0 Answers
Help!!!!getting setup 1 Answer
Unity server/client player attachments parenting 1 Answer
Instantiating multiple prefabs? Why? 0 Answers
Ground check BoxCast is detecting when player hits bottom of platform. UNITY 2D C# 0 Answers