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 Donovan_Rucastle · Oct 29, 2011 at 10:31 AM · objectspawncheckdestroyed

check if object is destroyed ?

Hi All I have this script to spawn my object repeatedly after a delay

var Theprefab : GameObject; var WaitTime = 1.0;

function Start () { InvokeRepeating ("spawn", WaitTime, WaitTime);

}

function spawn () { var Instance : GameObject = Instantiate(Theprefab, transform.position, transform.rotation); } It works prefectly it spawns the object and has a delay then spawns the object again but how can I check if the previous object is destroyed before spawning the new one ?

On the objects script when I click on the object it destroys itself so how would I check if the object is still in the scene before spawning the new object ?

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

3 Replies

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

Answer by aldonaletto · Oct 29, 2011 at 01:01 PM

You should change the logic, since InvokeRepeating can't be temporarily suspended - you can just cancel it with CancelInvoke. You could use a coroutine instead, and call Spawn only the object count is below the max (1, in this case).
About the object count: let Unity take care of this for you! The idea is simple: attach the spawning script to an empty object, which will be the parent of every spawned object; Unity always knows how much children a transform has - the childCount property is incremented and decremented automatically when a child is created or destroyed - thus you can read its childCount property to know how much objects are alive in scene. NOTE: The only restriction is that this empty object must stay static - no move, no rotation, or else all objects spawned will be moved as well!

var Theprefab : GameObject; var WaitTime = 1.0; var maxObjects: int = 1; // max spawned objects alive in scene

function Start () { while (true){ // let Unity free till the end of WaitTime yield WaitForSeconds(WaitTime); // spawn objects only when there are less than maxObjects if (transform.childCount < maxObjects) Spawn(); } }

function Spawn () { var Instance : GameObject = Instantiate(Theprefab, transform.position, transform.rotation); Instance.transform.parent = transform; // make it a child of its creator }

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 Donovan_Rucastle · Oct 29, 2011 at 01:27 PM 0
Share

Thank you so much I would have never thought of using childcount m

avatar image Donovan_Rucastle · Oct 29, 2011 at 01:33 PM 0
Share

Now the script works great but when I spawn the object for the first time it spawns immediatley how can I make it have the same delay as when it respawns?

avatar image aldonaletto · Oct 29, 2011 at 04:21 PM 0
Share

Oh, I forgot to change the order of the lines: place yield first and the if after it. I modified the answer to fix this.

avatar image aldonaletto · Oct 31, 2011 at 12:07 AM 1
Share

@Donovan_Rucastle, if this answer solved your problem, please click the accept button (the check mark button below the voting thumbs) - this will help others to find the solution for similar problems.

avatar image
1

Answer by col000r · Oct 29, 2011 at 12:34 PM

 var TheObject : GameObject;
 var Theprefab : GameObject;
 var WaitTime = 1.0;
 
 function Start () {
      InvokeRepeating ("spawn", WaitTime, WaitTime);
 }
 
 function spawn () {
      if(TheObject == null) Debug.Log("Has been destroyed!");
      TheObject = Instantiate(Theprefab, transform.position, transform.rotation);
 }
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
1

Answer by Donovan_Rucastle · Oct 30, 2011 at 04:02 PM

OhK now I have another problem :/

When using either one of the script there is a problem ! The first script it work fine but if I have multiple instances of the object open then the time delay doesn't work and spawn immediatley

In the second script it does wait until the object is destroyed at all it just keeps respawning them.

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 syclamoth · Oct 31, 2011 at 12:51 AM 0
Share

You see what you did just there? Ins$$anonymous$$d of closing off this question and opening a new one, you posted a comment as an answer and didn't mark either of the existing answers as accepted.

Don't do that.

avatar image aldonaletto · Oct 31, 2011 at 03:14 AM 0
Share

@syclamoth is right: posting comments/replies as answers mess things up.
Anyway, I tested my script and it works fine - a new object is spawned only after the other has been destroyed.

avatar image Donovan_Rucastle · Oct 31, 2011 at 01:26 PM 0
Share

Ok sorry for posting it as an answer

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

MissingMethodException error while trying to play animation when GameObject is destroyed 0 Answers

Problem with Intantiate using uLink! 2 Answers

Place object in place of ghost. 0 Answers

Spawn object with different material. 0 Answers

Number of spawned objects 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