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 KrKOo · Aug 11, 2017 at 01:43 PM · instantiatetimerobjectsvalues

Same script on multiple objects.

Hi! I'm trying to make a game where objects will respawn in 5 seconds after it was destroyed. I have this script attached on empty gameobjects on which the objects are spawning. The problem is, that it is not working when i have more of these empty gameobjects. Can someone help me please ??

     public GameObject Item;
     float spawnTime = 5;
     float time = 0;
     public bool spawned;
 
 
     void Start ()
     {
         Instantiate(Item, gameObject.transform.position, Quaternion.identity);
         spawned = true;
     }
 
     void Update()
     {
 
         if (spawned == false)
         {
             time += Time.deltaTime;
             if (time >= spawnTime)
             {
                 
                 Instantiate(Item, gameObject.transform.position, Quaternion.identity);
                 time = 0;
                 spawned = true;
             }
 
         }
     }

Comment
Add comment · Show 1
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 ShadyProductions · Aug 11, 2017 at 01:56 PM 0
Share

Ins$$anonymous$$d of destroying objects, simply disable them and re-enable them later. It will do good to your performance. Look at object pooling.

2 Replies

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

Answer by TwinGhosts · Aug 11, 2017 at 05:23 PM

Don't destroy them, you could use something like object-pooling to get more control over the objects. But aside from that, if you have a set amount of Object you could make them inactive outside of the screen and reactivate them again when they respawn.

I've made some test code for you to experiment with - it should work when you put this on your GameObject that has to respawn. You just need to write code which can make it die though.

 using UnityEngine;
 
 public class Dissapear : MonoBehaviour {
 
     float respawnTime = 5f;
     Vector3 respawnPosition;
     bool isAlive = true;
     
     // Update is called once per frame
     void Update () {
         
         // Check whether the object is alive or not and when it is not try to respawn it
         if (!isAlive) {
             
             // If it isnt trying to respawn yet, make it respawn
             if (!IsInvoking ("Respawn")) {
                 Invoke ("Respawn", respawnTime); // Respawn the object after 5 sec
             }
 
             // Deactivate the object because it is not active
             transform.position = new Vector3(-1000f, -1000f, transform.position.z);
 
         } else {
             //TODO actions when it is active
 
             // Set the respawnPosition for as long as it is alive
             respawnPosition = transform.position; 
             // You can call this once when it dies, but itś just to show you how it can be done.
         }
     }
 
     void Respawn(){
         isAlive = true; // Set the object back to alive so that it can die again
         transform.position = respawnPosition; // Set it back to its original position when it died
     }
 }
 




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 KrKOo · Aug 11, 2017 at 02:52 PM

OK, now i have this script, but when I "destroy" more objects at the same time, only the last one will respawn. Any ideas what should I do ?

     float spawnTime = 5;
     float time = 0;
     bool timer = false;
 
     GameObject Item;
 
     void Start ()
     {
        
     }
 
     void Update()
     {
         if (timer == true)
         {
             time += Time.deltaTime;
             if (time >= spawnTime)
             {
                 Item.SetActive(true);
                 timer = false;
                 time = 0;
             }
         }        
     }
 
     public void setSpawned(GameObject obj, bool spawned)
     {
         Item = obj;
         obj.SetActive(spawned);
         timer = true;
        
     }


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

84 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 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 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 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

how to pass values between objects in C# 2 Answers

Passing random values to a client. 0 Answers

Im trying to make an Timer within a for loop, cant figure it out. 1 Answer

Adding different values to randomly created objects 1 Answer

Need to spawn a gameobject from an array 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