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 QWERTY · Apr 10, 2010 at 03:51 PM · javascriptgameobjectrespawn

How to respawn a game object after a certain amount of time.

I am making a game using javascript. I was wondering how to make a game object respawn after a certain amount of time. the idea is the character collects the object and then after about 20 seconds the object respawns at the same point. The character can then collect the object again.

My original script is the following: private var timeSinceLastCollision = 0;

function OnControllerColliderHit(hit:ControllerColliderHit){

 if(hit.gameObject.tag == "PowerUp(smaller)" && timeSinceLastCollision <= 0) 
 {
     Destroy(hit.gameObject);
     transform.localScale = Vector3(transform.localScale.x * .5, transform.localScale.y * .5, transform.localScale.z * .5);
     timeSinceLastCollision = 5;
     //Wait at least 5 seconds between collisions
 }
 timeSinceLastCollision -= Time.deltaTime;    

}

so where do I put the script you wrote and what do I put in the spot where you wrote (collecteditem)

Comment
Add comment · Show 3
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 straydogstrut · Apr 14, 2010 at 02:33 AM 0
Share

collectedItem would just be a boolean that is initially false, and you would set it to true when the player picks up the item. Once it becomes true, the timer would start counting until it reaches it's limit and respawns the item.

avatar image QWERTY · Apr 14, 2010 at 04:07 PM 0
Share

I could not figure out how to make your way work but I did figure out how to make the object spawn after a certain amount of time.

avatar image straydogstrut · Apr 16, 2010 at 03:28 PM 0
Share

Glad you got it working, maybe post your new code as an answer in case someone else has a similar problem?

2 Replies

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

Answer by straydogstrut · Apr 10, 2010 at 05:40 PM

The Robot Guards of the 3D Platformer Tutorial on the Unity website respawn when the player can no longer see them. They are Instantiated from a prefab on this line:

currentEnemy = Instantiate(enemyPrefab, transform.position, transform.rotation);

Have a look at the tutorial I've linked to above and instantiating prefabs in the Unity Manual.


To respawn the item after a certain amount of time, you could start a timer once the player collects the object. Once the timer reaches the time limit you have specified, you can then respawn the object. Something like the following in your Update function:

if(collectedItem){      
    respawnTimer += Time.deltaTime;
    if(respawnTimer > delayTime){
        var newObject = Instantiate(objectPrefab, transform.position,
        transform.rotation);
        respawnTimer = 0.0;
    }
}
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 QWERTY · Apr 12, 2010 at 01:25 AM 0
Share

how do I make it so that the object does not appear until a set time?

avatar image
0

Answer by HardStyle · Apr 30, 2012 at 07:40 PM

is better to just hide the object or something like that..

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

1 Person is following this question.

avatar image

Related Questions

How to correctly convert Object to GameObject 1 Answer

Switching Gameobjects in a variable 1 Answer

Need help with logic problem (JavaScript) 1 Answer

Non-hard-coded object reference? 2 Answers

Load Prefabs in Array with Javascript 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