What happens when you Instantiate a GameObject?
I'm continuing work on my 2d platformer. However, In my Respawn script I call to Instantiate a new HeroGuy. When you plunge to your death on the cliff, he respawns successfully, his lives variable ticks down successfully, but he falls through the floor. This time, he doesn't even trigger my Destroying Collider! He's equipped with a RigidBody, a Box Collider, and a circle collider, and even inspecting the clone while the application is paused shows the this new (Clone) has all of the components of the old HeroGuy, so what gives?
This is my current Respawn code. using UnityEngine; using System.Collections;
public class DestroyOnContact : MonoBehaviour {
public GameController gc;
public GameObject heroGuy;
public bool respawn;
public Vector2 spawnLocation = new Vector2(0, 1);
void OnTriggerEnter2D(Collider2D other) {
if (other.gameObject.layer == 9) { //This is the player layer
Debug.Log ("Hey, it's HeroGuy!");
gc.lives = gc.lives - 1;
respawn = true;
Debug.Log ("Hey let's get him out of the way");
}
Destroy (other.gameObject);
if (respawn) {
Instantiate(heroGuy, spawnLocation, Quaternion.identity);
respawn = false;
}
}
}
Thank you for your time!
Answer by btmedia14 · Jul 23, 2016 at 03:52 PM
Is it possible the spawnLocation is such that the instantiated object is placed partially through the floor. In which case the physics engine will cause it to fall through. Perhaps try adjusting it, say Vector2(0.10) until a value that places it above the floor.
Unfortunately not. 0, 1 is where HeroGuy starts when you first run the program. :(
It should be noted that also that the ground is simply A pair of overlapping Box Colliders and that HeroGuy and GrassyHill are on two separate layers (Player and Ground, respectively.)
Does the instantiated object occur on the layer expected? That is, immediately after instantiating:
GameObject hero = Instantiate(heroGuy, spawnLocation, Quaternion.identity);
hero.layer = = Layer$$anonymous$$ask.NameToLayer("HeroGuyLayer");
Just a thought.
Hmm. I gave it a try, but the compiler throws an error, saying an Object can't be implicitly converted to a GameObject. Not sure what to do to get around that. :(
That said, inspecting the (Clone) says its on the expected Layer ("Player").
Also the heroGuy being Instantiated by my script is a Prefab copy of the original HeroGuy.
Gave it a try with this code. Still no luck. :(
using UnityEngine;
using System.Collections;
public class DestroyOnContact : $$anonymous$$onoBehaviour {
public GameController gc;
public bool respawn;
public GameObject heroGuy;
public GameObject newBody;
public Vector2 spawnLocation = new Vector2(0, 1);
void OnTriggerEnter2D(Collider2D other) {
Debug.Log (newBody.layer);
if (other.gameObject.layer == 9) { //This is the player layer
Debug.Log ("Hey, it's HeroGuy!");
gc.lives = gc.lives - 1;
respawn = true;
Debug.Log ("Hey let's get him out of the way");
}
Destroy (other.gameObject);
if (respawn) {
newBody = Instantiate(heroGuy, spawnLocation, Quaternion.identity) as GameObject;
newBody.layer = Layer$$anonymous$$ask.NameToLayer("Player");
respawn = false;
}
}
}
This was the debug output:
9
Hey, It's Hero Guy!
Let's move him out of the way.
The Destroyer object doesn't appear to have triggered a second time at all. as you mentioned before, yes, both the ground and the destroyer (which is underneath the ground and surrounding area) are both simply colliders with sprites attached.
A possible thought: Couldn't I just make this? if(layer = 9) {respawn code} else {Destroy(other.GameObject)}
The only thing then is I don't know how to alter other's Transform in code. >_<
Ok, I whipped together a similar project to this scenario and was able to create something along the same lines under specific conditions. The Player triggers the ground collider the first time, is destroyed, a new clone player is created. However, I do get a second trigger from the clone which gives the impression it is falling through the ground.
The situation depends on which prefab is attached to the public GameObject heroGuy which is used to instantiate the clone. In the inspector, ensure the attached prefab is dragged from the Assets folders. This will always maintain a reference to create new clones.
If the heroGuy object is assigned from the hierarchy, the Player itself, the first call to OnTriggerEnter will have a reference, but that same reference is being destroyed. The call to instantiate within the same frame will work as the Destroy() function has yet to complete. The clone created does trigger a second OnTriggerEnter(), but the second instantiate will fail with a null value, since heroGuy no longer exists, and an error occurs - which gives the impression the cloned player fell through the ground.
This was one way I was able to emulate the effect you experienced. But I do see an error condition displayed in the console (null reference).
So, ensure the heroGuy object assigned to the ground's public variable is a true prefab from within the Assets folders.
Edit: one more thing: On the console displaying the Debug output, if the collapse flag is on any repeated messages will only show once with a counter to the right.
Your answer
![](https://koobas.hobune.stream/wayback/20220612085355im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problems with rotating the player on the Y axis C# 0 Answers
How to Parent a Camera to the Head Bone with No Camera Shake? 0 Answers
Player does not jump? 0 Answers
Test for cube with tag "Player" touch cube with tag "Finish", then switch scene to "Lvl2" 0 Answers
Trying to make a respawn function but it is not working. 0 Answers