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 kir1251 · Apr 23, 2014 at 06:12 PM · javascriptdestroy objectprimitives

Cannot Destroy() object, created via GameObject.CreatePrimitive (others destroyed as should)

Hi all. I'm completely new to Unity, installed it yesterday. I tried to create a cube via GameObject.CreatePrimitive(PrimitiveType.Cube); and then destroy it. And it don't disappears. When I doing the same code with object, placed on scene in editor or with object, gained from Instantiate, it destroys them as should.

Sample code to reproduce bug.

 var tmp : GameObject;
 function Update ()
   if(Input.GetKey(KeyCode.Z))
   {   
     tmp = GameObject.CreatePrimitive(PrimitiveType.Cube);      
   };
   if(Input.GetKey(KeyCode.X))
   {              
     UnityEngine.GameObject.Destroy(tmp.gameObject);      
   };
   if(Input.GetKey(KeyCode.C))
   {              
     UnityEngine.GameObject.Destroy(gameObject); 
   };
 }

if I add it to object on scene, C deletes the object, but X do not delete the cube created by Z.

Hope for your help!

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
Best Answer

Answer by Eric5h5 · Apr 24, 2014 at 07:04 AM

It's not a bug, or rather not a Unity bug; your code has several problems. First, the code won't do anything because you forgot a { after function Update (). If you'll notice, the script won't compile. Once you fix that, it sort of works, though you should replace GetKey with GetKeyDown, since otherwise it will spawn cubes every frame the key is down. Even with GetKeyDown, it's not ideal, since "tmp" is always a reference to the latest cube spawned, so destroying tmp only removes that reference, not any of the other cubes that may have been spawned.

Unrelated, you should remove references to UnityEngine, since that's automatically imported anyway. Also you can remove GameObject since Destroy inherits from Object. In other words, replace "UnityEngine.GameObject.Destroy" with just "Destroy". Also, you should remove the ; after your if statements. They don't hurt exactly, but they're not doing anything and could cause confusion.

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 kir1251 · Apr 24, 2014 at 07:21 AM 0
Share

Thanks for your responce. It's not actual code, just sample code to easy reproduce the bug. It has contained some errors, because it's just cutted from actual code, written for unity features testing, without any unrelated text.

The thing is, if I create primitive by function, and then destroy it as I do in X key block, its mesh is still on scene and in scene hierarchy window. If I replace GameObject.CreatePrimitive(PrimitiveType.Cube) to Instantiate(PrefabCubeVariable), where PrefabCubeVariable contains cube prefab, it's works properly and deletes all I need.

avatar image Eric5h5 · Apr 24, 2014 at 07:34 AM 0
Share

That's not what happens though. There is no (Unity) bug, as I mentioned. Once you fix the code you posted as I described, it works, as long as you only spawn one cube.

avatar image kir1251 · Apr 24, 2014 at 07:42 AM 0
Share

Ok, sorry, looks like you're right now. This problem actually was with Get$$anonymous$$ey. Thanks for help.

But it's still not solve the bug I tried to reproduce. When I create blocks and then them aren't delete. :( The code there looks like that:

 function Start () {
     groundBlocks = new GameObject[CHUN$$anonymous$$_SIZE * CHUN$$anonymous$$_SIZE];
     //create plane ground TODO: noise;
     for (var ix = 0; ix < CHUN$$anonymous$$_SIZE; ix ++) 
           for (var iy = 0; iy < CHUN$$anonymous$$_SIZE; iy ++) {
               groundBlocks[ix * CHUN$$anonymous$$_SIZE + iy] = Instantiate(BlockPrefab);//GameObject.CreatePrimitive(PrimitiveType.Cube);
               groundBlocks[ix * CHUN$$anonymous$$_SIZE + iy].transform.position.x = x * CHUN$$anonymous$$_SIZE + ix; 
               groundBlocks[ix * CHUN$$anonymous$$_SIZE + iy].transform.position.z = y * CHUN$$anonymous$$_SIZE + iy;
               groundBlocks[ix * CHUN$$anonymous$$_SIZE + iy].GetComponent($$anonymous$$eshRenderer).material = Grass$$anonymous$$aterial;
           }      
     
 }
 
 function OnRemove() {
     for (var gbi = 1; gbi < CHUN$$anonymous$$_SIZE * CHUN$$anonymous$$_SIZE; gbi++) {
         UnityEngine.Object.Destroy(groundBlocks[gbi]);
     }
     UnityEngine.Object.Destroy(gameObject);
 }

And, when I call OnRemove on that script, all blocks not disappearing from scene.

avatar image kir1251 · Apr 24, 2014 at 09:39 AM 0
Share

Okay, already solved this. The two errors is: 1) calling Start() manually 2) indexing from 1 in line 15

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

21 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 avatar image avatar image avatar image avatar image

Related Questions

The first object in selection grid can't be activated. Why? 1 Answer

How to remove a word from string 0 Answers

Help Needed! expecting (, found 'Chop'. 1 Answer

Loading next level in js 1 Answer

In unity Free ,is pc the only device i can use my game and if i want to sale it do i need a license ? 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