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 Msurdej · Dec 04, 2012 at 03:00 AM · c#collisioninstantiatedestroy

Removing an object if it spawns in a building

In my game, I have it that when pick-ups are collected, a new one is made in a random location in the game space. However, sometimes this location is inside of a building, so the player cannot reach it. I have the following code on my buildings to that if anything with the tag respawn(the pick-ups) enter their collision box, they're destroyed, and a new one is made.

using UnityEngine; using System.Collections;

 public class Building : MonoBehaviour
 {
     public GameObject parson;
     void OnTriggerEnter (Collider other) 
     {
             if(other.gameObject.tag == "Respawn")
         {
             Debug.Log ("In BUILDING");
             
              Destroy(other);
              GameObject newparson = Instantiate(parson, new Vector3(Random.Range(10,-35),921,Random.Range(267,310)), Quaternion.identity)as GameObject;
              parson = newparson;        
         }
     }
 }

However, this sometimes lead to the creation of pick-ups that don't get collected(their supposed to follow the player, but these seem to stay put, and allow the player to constantly rack up points). I want it so that everytime a pick up is in a building, the pick up is removed, and a new one is created. Any help would be appreciated.

Comment
Add comment · Show 1
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 ThePunisher · Dec 04, 2012 at 06:26 AM 0
Share

Yeah, $$anonymous$$arkFinn is correct. The line "Destroy(other)" is destroying the Collider component on that GameObject rather than the GameObject itself. Use the code $$anonymous$$arkFinn gave you and that should be good.

Also, it might be more useful to develop a spawn system that would prevent such things from happening, rather than dealing with the consequences.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by MarkFinn · Dec 04, 2012 at 06:04 AM

I'm only guessing here, but could it be that you need to say

Destroy(other.gameObject);
so that you're not only destroying the collider?

Only a guess...

Comment
Add comment · Show 3 · 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 Msurdej · Dec 08, 2012 at 01:07 AM 0
Share

Doing that seems to get me the following error when i try to pick up one spawned inside a building:

$$anonymous$$issingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.

I have tried moving the piece of code which deletes the objects to the end of the code, but that doesn't seem to work. This also seems to destroy previously picked-up objects, which is something I don't want happening ><.

avatar image MarkFinn · Dec 08, 2012 at 10:58 AM 0
Share

I'm not really getting what you want to happen in this situation. You spawn a pickup in an invalid location, delete it and then try to pick it up?

Something strange in your logic, or at least in my understanding of it.

avatar image Msurdej · Dec 08, 2012 at 07:23 PM 0
Share

I want it to remove the pick up, make a new one, and be able to pick the new one up.

However, last night I had an idea: What if ins$$anonymous$$d of destroying the pick-up, I just have it get moved again?

Turns out my late night code idea worked! by moving the object ins$$anonymous$$d of deleting it and then making a new one, I got objects that spawn in buildings spawning in a new location.

 other.gameObject.transform.position = new Vector3(Random.Range(10,-35),921,Random.Range(267,310));

Thank you for the help!

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

11 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

Related Questions

Destroy temp object and make new object. 0 Answers

Instantiate 1 object after 2 objects collide. ( C# ) 1 Answer

Destorying prefab and instanstiating again? 0 Answers

"Destroy" bullet without turning off trail renderer 2 Answers

Multiple Cars not working 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