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
2
Question by RAWTalent · Mar 02, 2011 at 07:00 PM · objectspawnkeyhidepress

Key press hides object and spawns another

I have a playable character, I have it set to that on a key press he damages an enemy, but what I want is a script that will hide my character for 3 seconds and spawn an object in it's place and then after the 3 seconds destroy the spawned object and unhide the character. Any ideas?

    function Update () {
gameObject"spinny".active = false;
if (Input.GetKey ("fire1") && Time.1 >)
        gameObject"spinny".active = true;
        }

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

2 Replies

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

Answer by Statement · Mar 03, 2011 at 01:54 PM

I guess you could do something similar to this (but make sure to read Joshuas links, not all will be free):

// Set this to the prefab you want to create. var inPlacePrefab : GameObject;

function Start() { // Thanks to Derek that pointed out we can't use Update. // The code has been revised, now we use another Update routine. StartCoroutine(UpdateInput()); }

function UpdateInput() { while (true) { if (Input.GetButton("Fire1")) { yield HideAndSpawn(); } else { yield; } } }

function HideAndSpawn() { // 1. Hide self with disabling renderer. renderer.enabled = false;

 // 2. Spawn that prefab in our place.
 var spawned = Instantiate(inPlacePrefab, transform.position, 
                                          transform.rotation);

 // 3. Wait 3 seconds.
 yield WaitForSeconds(3);

 // 4. Destroy the spawned item.
 Destroy(spawned);

 // 5. Show self with enabling renderer.
 renderer.enabled = true;

}

Comment
Add comment · Show 8 · 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 PrimeDerektive · Mar 03, 2011 at 02:00 PM 1
Share

Wouldn't that yield in Update give you the old "Update cannot be a coroutine"? Also even if it did work it would start a new HideAndSpawn() coroutine every frame you had Fire1 depressed, wouldn't it?

avatar image Statement · Mar 03, 2011 at 02:03 PM 0
Share

Oh darn, you might be right.

avatar image Statement · Mar 03, 2011 at 02:07 PM 0
Share

I created another method UpdateInput for this task. Putting the whole update loop in a coroutine will stop the code in HideAndSpawn until it is completed, so it can't launch several HideAndSpawn running in parallel.

avatar image Statement · Mar 03, 2011 at 02:16 PM 0
Share

$$anonymous$$ake sure to see my updated answer, I haven't tested it myself (shame on me) but to me it looks like it would work now :)

avatar image RAWTalent · Mar 03, 2011 at 02:20 PM 0
Share

Yeah it works, i'll edit it a bit to hid all of the rendered meshes in my main character, and some how make it move with my character :S, much appreciated

Show more comments
avatar image
2

Answer by Joshua · Mar 02, 2011 at 08:36 PM

Just break it down into simple things. I'm pretty sure every step of the way has already been answered somewhere on this site.

How to execute a command on pressing a key.,

How to make something invisible.,

How to instantiate an object.,

How to set a timer.,

How to make something happen when timer reaches a certain point.

How to destroy an object.,

How to make something invisible visible again..

If there's a specific step you're having trouble with I'd be more then happy to help, that's what this site is here for. But we're not here to write free scripts for you.


Edit: when I wrote this the original question was a LOT less detailed, so ignore the tone of the message :) not deleting the actually post because the links might still be useful.

Comment
Add comment · Show 6 · 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 RAWTalent · Mar 03, 2011 at 01:43 PM 0
Share

Sorry was in a bit of a hurry when posting, I've updated the first post with my script so far, it's not working though :S

avatar image Statement · Mar 03, 2011 at 01:45 PM 0
Share

No, we're not here to write free scripts, but having both approaches doesn't maim :)

avatar image RAWTalent · Mar 03, 2011 at 01:54 PM 0
Share

I'm sorry I'm not quite getting what you mean?

avatar image Statement · Mar 03, 2011 at 01:58 PM 0
Share

I commented on Joshuas remark "But we're not here to write free scripts for you.", I didn't reply you.

avatar image Joshua · Mar 03, 2011 at 04:33 PM 0
Share

Heh Statement, when I posted this the question was a little less.. explicit so you may have gotten the wrong impression when you read it. ;) kudos on writing the script though, def +1 from me.

Show more comments

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

No one has followed this question yet.

Related Questions

How to get raycast and key click info. 1 Answer

Rotating an Object on Key Press! 3 Answers

Rotate on Key press help? 1 Answer

Hide GUI Label when click in open space or on other objects 1 Answer

Spawning if there is space 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