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 jokesman · May 26, 2011 at 03:38 PM · prefabshootdestroyed

How to shoot only once

I would really apreciate if somebody gave me a script which could help me shoot an object like this: I am using a prefab to shoot a football but i want the shooting to stop until the ball is destroyed.So i need only one ball shooted and when it is destroyed i'll be able to shoot the next one... :/

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

5 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by homeros · May 26, 2011 at 04:51 PM

Do it like this:

 var spawnedBall:GameObject;

when instantiating use it like this:

 if(spawnedBall == null)
 spawnedBall = Instantiate(prefabBall);

This way it'll check if the gameobject is null first, if it is, it'll instantiate a ball and reference your object. When ball you spawned is destroyed, spawnedBall will be null again.

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 Wolfram · May 26, 2011 at 05:07 PM 1
Share

Note that everything related to shooting the ball (applying a force, swinging the foot, etc.) goes into the body of the "if(spawnedBall == null)", so that it is ONLY executed if you actually are allowed to shoot the ball.

avatar image homeros · May 26, 2011 at 05:11 PM 0
Share

they shouldn't be inside "if(spawnedBall == null)" statement, because he can shoot if the ball is spawned either when it's just spawned or spawned before.

edit: oh I just understand what he wants exactly. dunno why but I thought it was like a football game or like. so you're probably right about if statement.

avatar image Wolfram · May 26, 2011 at 05:16 PM 0
Share

...or I misinterpreted his phrasing ^^

You would be correct if by "i want the shooting to stop" he means, he doesn't want to spawn any more balls, but still wants to be able to interact with the ball by kicking it around.

avatar image
1

Answer by Wolfram · May 26, 2011 at 03:41 PM

Store the instantiated ball in a variable, reset this variable to null on destruction, and only spawn a ball if variable==null

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 jokesman · May 28, 2011 at 05:54 PM

Here is what i've done.My problem is that the ball is never destroyed :(:(... Only after it's destruction i want it to be able to shoot. If anyone can help i would really apreciate it.

 var shootForce:float;
 var parentPlayer:Transform;
 var prefabBall:Transform;
 var spawnedBall:GameObject;
 var check:boolean;
 check=true;
 function Update()
 {
 if(Input.GetMouseButtonDown(0) &&(check == true))
 {
 check=false;
 var instantiatedBall = Instantiate(prefabBall, transform.position, Quaternion.identity);
 instantiatedBall.rigidbody.AddForce(transform.forward * shootForce);
 spawnedBall=instantiatedBall;
 Destroy(spawnedBall,2);
 check=true;
 }
 }
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 jokesman · May 30, 2011 at 11:37 AM 0
Share

Can anyone help please???I don't think it's that difficult ! :(

avatar image
0

Answer by jokesman · May 30, 2011 at 04:49 PM

Can anyone help here ?? The only thing i can't do is to destroy the ball !!What's wrong please??How can i use destroy here ? See my code above.

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 homeros · May 30, 2011 at 04:59 PM 0
Share

for some reason you don't seem to read our answers? both me and wolfram told this. use spawnedBall == null and delete check == true. it doesn't work anyway in the state you're using it.

avatar image jokesman · May 30, 2011 at 05:05 PM 0
Share

Ok thank you ! But do you have any idea why the ball is not destroyed? What's wrong with Destroy(spawnedBall,2)?

avatar image homeros · May 30, 2011 at 07:03 PM 0
Share

I don't know why but what I usually do is like this. I use a script on the spawned object, in it i define an explode function. WaitForSeconds(2);Destroy(gameObject); Then I call it from the start function. This way the ball will be destroyed 2 seconds after its spawned.

avatar image Wolfram · May 30, 2011 at 07:08 PM 0
Share

As we told you repeatedly, your current approach doesn't work. With Destroy(...,2) you delay the destruction by 2 sec. However, code execution continues immediately, so "check" is immediately cleared, which will spawn a new ball and overwrite spawnedBall on your next mouse click.

In my very first response to an answer you deleted prematurely I would have told you to:

  • don't use delayed Destroy if you need to combine it with game logic

  • write a separate coroutine which waits for 2 seconds first and then destroys the object without delay, and resets "check" to true.

You can also skip this "check" variable altogether if you ins$$anonymous$$d follow Speed's and my advice we gave you 4 days ago in the answers above.

avatar image
0

Answer by jokesman · Jun 03, 2011 at 04:10 PM

I do it but the ball is never destroyed.Please write the small script about how to destroy the ball ............:( i can't go on with the project and i have to .. Thanks for the answers.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to shoot a bullet to curser position? 2 Answers

Shoot Script 1 Answer

My object acts wierd, 2 balls being shot instead of one (from different position) 2 Answers

Shooting problem - bullet shoots diagonally when facing forward 2 Answers

When flipping player, instantiated objects spawn on different spot. 2D shooting 0 Answers


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