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
1
Question by jkish1990 · Sep 08, 2012 at 03:02 AM · instantiatedestroynot workinggrid

Objects will not destroy after they have been instantiated

So here is my code. I am not a programmer so I probably have done quite a few things wrong. I am an artist and this is a prototype for a project I hope to create. So I create these objects randomly (for demo purposes they are cubes and spheres) and after a certain time I want them to be destroyed and new objects replace them. However instead of the clones being destroyed the gameobject this script is attached to is destroyed.

var grid: int = 4; //size of grid

var randomnum : float = 0.1; //random number generator

var isSpawned : boolean = false;

var spawncubePrefab : GameObject; //cube

var spawnspherePrefab : GameObject; //sphere

function Start () {

}

function Update () {

if (isSpawned == true){

return;

}

else {

CreateGrid();

isSpawned = true;

DestroyClones();

} }

function CreateGrid() {

for (var i=1; i<(grid+1); i++){

var zpos= (2.5*((i*i)+i+4)); //creates rows at certain distance

for (var j=0; j randomnum) {

Instantiate(spawncubePrefab, Vector3(xpos,2,zpos), transform.rotation);

Debug.Log("I created a cube");

}

else {

Instantiate(spawnspherePrefab, Vector3(xpos,2,zpos), transform.rotation);

Debug.Log("I created a sphere");

}

}

}

}

function DestroyClones() {

var randomtime = Random.Range(3,7);

print (randomtime);

yield WaitForSeconds (randomtime);

Destroy(gameObject);

isSpawned = false;

Debug.Log("Destroyed");

}

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

Answer by aldonaletto · Sep 08, 2012 at 03:48 AM

@jkish1990, this damned UA text editor seems to have eaten part of your code, in the j loop. I don't know what exactly it was before, but tried to calculate xpos in some logical manner, and selected the prefab randomly based on a 50%/50% cube/sphere ratio.
Anyway, you were destroying gameObject, which's the reference to the object to which this script is attached - you should instead destroy the objects you have created, but this is impossible at DestroyClones because you don't have references to the objects! Fortunately, there's a simple solution in this case: call Destroy(clone, randomtime) right after creating an object (where clone is a reference to the object created) - this will kill the object after a randomtime delay. Since all objects are created in the same frame, they will also die in a single frame, randomtime seconds after - just wait the randomtime delay and clear the variable isSpawned:

var grid: int = 4; //size of grid var isSpawned : boolean = false; var spawncubePrefab : GameObject; //cube var spawnspherePrefab : GameObject; //sphere

function Update () { if (isSpawned == true){ return; } else { CreateGrid(); isSpawned = true; } }

function CreateGrid() { var randomtime = Random.Range(3,7); // generate the random life time for (var i=1; i<(grid+1); i++){ // create the objects var zpos= (2.5*((i*i)+i+4)); //creates rows at certain distance for (var j=0; j < grid; j++){ var xpos = 2.5 * j; //creates columns at certain distance var prefab: GameObject; if (Random.value >= 0.5) { // select the prefab randomly prefab = spawncubePrefab; } else { prefab = spawnspherePrefab; } // create the object and get a reference to it: var clone: GameObject = Instantiate(prefab, Vector3(xpos,2,zpos), transform.rotation); // program the object destruction time: Destroy(clone, randomtime); } } yield WaitForSeconds(randomtime); // wait randomtime... isSpawned = false; // then clear isSpawned, since all objects have suicided by now }

Comment
Add comment · Show 1 · 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 jkish1990 · Sep 09, 2012 at 01:04 AM 0
Share

Thank you for your swift answer. I really appreciate your help and I am proud to say it works!!

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

9 People are following this question.

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

Related Questions

how to destroy camera instatiated from prefab? 0 Answers

Keeping track of which areas remain unexplored 1 Answer

Which is faster for a pool of objects, instantiating and adding to an array or loading pre-made objects to an array? 1 Answer

Could someone show me examples of how to use void OnDestroy() to instantiate an object? 2 Answers

Destroy Instantiated GameObject Problem 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