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 Hybrid1969 · Oct 17, 2013 at 09:18 PM · instantiateprefabdestroy

Instantiated Prefab doesnt destroy

really odd issue.

Im Instantiating a dice prefab and then destroying the prefab and re instantiating to roll again with OnGUI button click.

the main issue is that after the 2nd destroy/instantiation the prefabs are no longer destroyed or registered.

code is -

 void DieSpawn()
     {
         
         if (GameObject.FindGameObjectWithTag("Die"))
         {
             //GameObject.Destroy(dieGameObject);
             
             Destroy(GameObject.Find(dieName));
 
             //dieGameObject = null;
         }
         
         // create the die prefab/gameObject
         dieGameObject = (GameObject)Instantiate(diePrefab,spawnPoint.transform.position ,Quaternion.identity)as GameObject;
         
         // give it a random rotation
         dieGameObject.transform.Rotate(new Vector3(Random.value * 300, Random.value * 300, Random.value * 300));
         
         //create reference to gameobject
         dieGameObject = GameObject.Find(dieName);
         dieValueComponent = dieGameObject.GetComponent<DieValue>();    
                 
         //apply force
         dieGameObject.constantForce.force = new Vector3(1, 0, 1);
     }

ideas as I just cant see whats wrong with this...

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

2 Replies

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

Answer by Hybrid1969 · Oct 17, 2013 at 11:08 PM

solved the issue.

just found the solution while looking at another script for answers to another problem.

changed - Destroy(dieGameObject);

to - DestroyImmediate(dieGameObject);

and problem solved

Comment
Add comment · Show 4 · 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 meat5000 ♦ · Oct 17, 2013 at 11:10 PM 0
Share

Was it this one? :D

Be careful with DestroyImmediate. It can permanently destroy assets!

avatar image meat5000 ♦ · Oct 17, 2013 at 11:13 PM 0
Share

Just noticed there's a whole answer and a bunch of comments missing from this page.... they've just gone. $$anonymous$$y comment was

The object won't be destroyed until next frame so repopulating the variable-object you just destroyed may cause problems.

avatar image Hybrid1969 · Oct 18, 2013 at 12:23 AM 0
Share

thanks for putting your original comment back...I made a bit of mess of this questions.

DestroyImmediate currently seems to be the only solution currently

avatar image meat5000 ♦ · Oct 18, 2013 at 12:31 AM 0
Share

I had to resort to it after I had a similar problem between Destroying and Instantiating in the same frame.

It works but be careful with it. DestroyImmediate actually comes with a warning in the scripting reference. Besides asset destruction it can cause problems with NullRefs etc.

avatar image
0

Answer by chillersanim · Oct 17, 2013 at 09:39 PM

When instantiate an object, Unity adds the string "(clone)" behind his name.
If you'r property "dieName" is only the original name (without "(clone)" ), then it will not find any gameobject on the line 8 of your code.

Instead, you can rename the instantiated object so that the "(clone)" is removed -> the gameobject is being found:

 dieGameObject.Name.Replace("(Clone)", string.Empty);

I'm not sure why you have added the line 20, because the dieGameObject is already referencing the desired object.

If it is NOT because of the name, then there are other possibilities what could have gone wrong

  1. The prefab doesn't have the correct tag

  2. Some other script manipulates the tag or name of the object

At least, I would reccomend you, not to use different methods to find an object (like GameObject.FindGameObjectWithTag("Die") and GameObject.Find(dieName))
Then you can at least be sure, that the problem is with the choosen method.

You can also debug to check if it enters the if on line 4, and if it finally finds the dieObject.

Hope I was able to help

Greetings
Chillersanim

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

16 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

Related Questions

Can't remove instantiated prefab 0 Answers

how to destroy camera instatiated from prefab? 0 Answers

Instantiated gameObject(here, wall) destroyed, but when checked for existence, shows that it exists, how? 1 Answer

Removing A Component From An Instantiated Prefab After X More Are Instantiated 1 Answer

Destroying an instantiated prefab particle effect 4 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