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 /
avatar image
0
Question by gamerbrain84 · Jan 20, 2016 at 01:01 PM · instantiatedestroyprefabs

Can't Destroy Instantiated Prefab

Hey all,

I'm sure that this has been asked another way but not in any way that I comprehend as being similar, so excuse that.

I'm trying to do a base builder, am relatively new to Unity, and have a problem destroying the 'tent' object that I'm creating in order to replace it with a new, upgraded tent model.

Here is the script that is on the empty container gameobject 'tent':

 public class Tent : MonoBehaviour
 {
     int tier = 0;
     public GameObject[] models = new GameObject[3];
     public GameObject currentModel;
 
     //This is a proxy solution to having the building appear as the lowest tier. Need a way to save.
     void Awake()
     {
         currentModel = Instantiate(models[0], new Vector3(this.transform.position.x,
             this.transform.position.y, this.transform.position.z), Quaternion.identity) as GameObject;
     }
 
     public void UpgradeTent()
     {
         if (tier < 3)
         {
             tier++;
             Destroy(currentModel);
             currentModel = Instantiate(models[tier], new Vector3(this.transform.position.x,
             this.transform.position.y, this.transform.position.z), Quaternion.identity) as GameObject;
         }
     }
 }

It should instantiate the tent model (another empty game object with loads of models as children) upon awake. Then when the upgrade function is called, ditch the old model and replace it with the swanky new model from the array.

Both the tent object (with the tent script on it) and the tent model are prefabs, which I'm pretty sure is a big part of the issues.

Thanks in advance for any help.

Comment
Add comment · Show 3
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 VildNinja · Jan 20, 2016 at 01:31 PM 0
Share

Where are you calling the UpgradeTent function? and does the new tent spawn as it's supposed to? It shouldn't matter that both are prefabs.

avatar image Bonfire-Boy VildNinja · Jan 20, 2016 at 03:17 PM 1
Share

It could matter if the UpgradeTent function was being called on a prefab rather than an instance (so your initial question looks like a good one to me).

The other questions I would want to ask are

1) what is actually happening? For example, the question just says that an instantiated prefab (presumably the old current$$anonymous$$odel but it would be better if this was made explicit) isn't being destroyed, but is the new current$$anonymous$$odel being correctly instantiated?

2) are any errors being logged or exceptions being thrown?

avatar image BigHandInSky · Jan 20, 2016 at 03:11 PM 0
Share

In addition to VildNinja's comment I'd suggest checking whether models[...] has GameObjects assigned in the inspector, I know I've missed that a couple of times by accident.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by StephanT · Jan 28, 2016 at 09:45 AM

Just a note to your instantiation code in lines 10 and 20:

 currentModel = Instantiate(models[0], new Vector3(this.transform.position.x,
                      this.transform.position.y, this.transform.position.z), Quaternion.identity) as GameObject;

You're creating a new Vector3 with the properties of transform.position. Well, you can just use that. And the this is redundant, too.

So, to simplify these two lines I suggest using:

 currentModel = Instantiate(models[0], transform.position, Quaternion.identity) as GameObject;



Comment
Add comment · 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
0

Answer by FortisVenaliter · Jan 27, 2016 at 06:39 PM

The code looks good to me.... The only recommendation I can give is that since you're using the same reference variable, it might be a good idea to use DestroyImmediate() rather than Destroy().

The Destroy() function won't actually delete the object until the end of the frame.

Other than that, I would agree with @Bonfire-Boy that you should check to make sure the function is being called, ideally by throwing a Debug.Log in there and ensuring you see it at the right time in the editor log.

Comment
Add comment · 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

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

7 People are following this question.

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

Related Questions

Instantiate Pre-fab clone altered on Instantiate 2 Answers

How to destroy an instantiated prefab in C#? 4 Answers

Only 1 of 3 conditions being executed in IF statement?(Solved) 1 Answer

Can't seem to destroy instantiated objects. 1 Answer

How to Instantiate a GameObject from a ScriptableObject piece of script? 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