Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by jordmax12 · Nov 27, 2013 at 01:44 AM · c#asteroids

gameObject destroying itself

So I have a gameObject 'playerShip' that is my ship in my asteroids re-make. For some reason whenever I try to instantiate it, weird stuff happens. When it collides with an asteroid it destroys itself, decrements p1Lives and then Instantiates a new playerShip. Sometimes it creates the playerShip, and then 2 seconds later after it moves it destroys itself. Sometimes it doesn't even spawn the asteroid. I don't know why, but here's some code to hopefully trigger someone's lightbulb as to why this is happening. If you need more code I will gladly post it.

 //player controller class 
     void OnTriggerEnter(Collider other){
         if(other.transform.tag == "a_large" || other.transform.tag == "a_medium" || other.transform.tag == "a_small")
         {
             GC.p1Lives--;
             GC.RespawnPlayer();
             Destroy (gameObject);
         }
     }
 //Destroy (gameObject) is refering to gameObject 'playerShip'
 
 //GameController class calls the RespawnPlayer method
     public void RespawnPlayer()
     {
         if(p1Lives > 0){
             if(!Physics.CheckSphere (Vector3.zero, 256f)){
             Instantiate(playerShip, new Vector3(0,0,0), Quaternion.identity);    
             //print (p1Lives);
                 CancelInvoke ("RespawnPlayer");
             }
         }
     }
 //i put in "print(p1Lives)" to see if it was problems with decrementing, but this is not the case. 
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
1
Best Answer

Answer by Roland1234 · Nov 27, 2013 at 04:00 PM

Would need more details on your setup to verify this, but are you passing in the reference of the gameObject that you are queueing up for destruction to the Instantiate method? That might account for the behaviour you're seeing. Try making your player ship into a prefab and pass that in to Instantiate instead.

I assume you have a way of accounting for that CheckSphere failing as well.

"Sometimes it doesn't even spawn the asteroid" - did you mean playerShip or are you actually needing to spawn another asteroid or something?

Comment
Add comment · Show 2 · 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 jordmax12 · Dec 04, 2013 at 01:24 AM 0
Share

Yeah sorry not asteroid, but 'playerShip' also, my playership is a prefab :(

avatar image Roland1234 · Dec 04, 2013 at 01:46 AM 1
Share

If you've verified that your colliders are set-up correctly then try:

  1. Add a bool to the player controller class, initialized to false, and add the logic to only destroy the ship if it is false (setting it to true the first time it has been queued up to Destroy).

  2. Remove the CheckSphere condition and add a timer to the player controller class ins$$anonymous$$d (initialized at something like 2 seconds) which will exit the OnTrigger$$anonymous$$ethod until it winds down to zero. Remove the CancelInvoke call as it shouldn't be necessary.

These may be considered just intermediary steps to try and narrow down the issue - if none of the above work then there's something else going on and I'd need to see the full scripts involved (and maybe more details about your scene setup).

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Flip over an object (smooth transition) 3 Answers

Achieving an Asteroids-style screen wrap 1 Answer

Making a bubble level (not a game but work tool) 1 Answer


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