Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by SigurdOfAlest · Jul 23, 2016 at 02:53 PM · c#playerrespawning

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SigurdOfAlest · Jul 23, 2016 at 03:59 PM 0
Share

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.)

avatar image btmedia14 SigurdOfAlest · Jul 23, 2016 at 04:39 PM 0
Share

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.

avatar image SigurdOfAlest btmedia14 · Jul 23, 2016 at 04:58 PM 0
Share

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.

Show more comments
avatar image SigurdOfAlest · Jul 23, 2016 at 10:55 PM 0
Share

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. >_<

avatar image btmedia14 SigurdOfAlest · Jul 24, 2016 at 02:37 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges