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 Obinsonray · Sep 10, 2011 at 11:07 AM · boolean

Basketball Game: Need Help!

I am trying to make it so that the player can only shoot once using a boolean, but the ball is still shot even after the boolean is set to true...

 var ballPrefab : GameObject;
 static var BallShot = false;
 function Update () {
 // Shooting Ball
 if(Input.GetMouseButtonUp(0)){
 
 if(BallShot == false){
 var Basketballs : GameObject = Instantiate(ballPrefab, transform.position, transform.rotation);
 var BallShot = true;
 Debug.Log(BallShot);
 }
 
 if(BallShot == true){
 Debug.Log("You don't have a ball!");
 }
 }
 }

What is wrong?

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
2
Best Answer

Answer by SisterKy · Sep 10, 2011 at 04:53 PM

but wait a moment... what's the var-keyword doing in front of BallShot = true;? You already define BallShot as static var at the beginning of the script. (I'm confused you don't have a compile-error...??)

Is the function that sets the BallShot to false in the other script a static function? Did you delete the script from your project altogether or just remove the instances of the component? Are you sure, you removed ALL instances?

Addition/Edit: Oops O_o Sorry @aldonaletto I posted this as an answer, then decided it should go in a comment after all, because I thought it was not really an answer but just a guess. But meanwhile aldo had commented and I didn't see it but deleted the answer (and his comment along with it)
Here's what he said:

That's the problem: the var keyword creates a local variable named BallShot and sets it to true - but the original BallShot is never changed. The var keyword must be removed - and BallShot must be cleared somewhere to allow new shoots.

Greetz, Ky.

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 Obinsonray · Sep 10, 2011 at 05:17 PM 0
Share

Ok that fixed it thanks, umm if I have another question do I create a new question? or do I continue form here

avatar image SisterKy · Sep 10, 2011 at 06:43 PM 0
Share

Please accept this question as 'the' answer (the little checkmark top left of this post) so that it will be noted 'green' and open a new question. If it's related you can post a link back to this question. Thank you for asking :) Greetz, $$anonymous$$y.

avatar image SisterKy · Sep 10, 2011 at 06:49 PM 0
Share

On second thought, it depends a bit. Sometimes if the 'other' question showed up as a immediate result of the last fix (like a compiler-error after you did what you were told) you could as well continue in the comments until this script works. But if it's only roughly related but not the very same script/problem, please open a new question.

avatar image
0

Answer by FLASHDENMARK · Sep 10, 2011 at 11:16 AM

 var ballPrefab : GameObject;
 static var BallShot = false;
 function Update () {
 // Shooting Ball
 if(Input.GetMouseButtonUp(0)){
 
 if(BallShot == false){
 var Basketballs : GameObject = Instantiate(ballPrefab, transform.position, transform.rotation);
 var BallShot = true;
 Debug.Log(BallShot);
 }
 
 if(BallShot == true){
 Debug.Log("You don't have a ball!");
 }
 }
 }

You are never setting ballShot to false. Are you sure that is not connected to the problem. Or are you referring to this variable in another script since it is static?

I would have made a comment instead of a answer, but I cant add a comment for some reason.

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 Obinsonray · Sep 10, 2011 at 11:31 AM 0
Share

Umm Yeah I do set it to false in another script, but right now I'm trying to start it off with false, then once it is thrown it is set to true and it cannot be shot again

avatar image SisterKy · Sep 10, 2011 at 12:35 PM 1
Share

I don't immediately see anything wrong with this script... are you sure this other script does not accidently set it to true as well? this might happen if you have BallShot != BallShot; to switch from true to false (but it will also switch from false to true)...? Greetz, $$anonymous$$y.

avatar image Obinsonray · Sep 10, 2011 at 02:51 PM 0
Share

I tried it without the other script, it still shoots the ball and the Debug.Log outputs with TRUE even when it is shooting

avatar image SisterKy · Sep 10, 2011 at 04:14 PM 0
Share

Well, of course it will print TRUE because you set it to true just one line above. Try to put the Debug.Log(BallShot); ABOVE var BallShot = true; and it will output FALS$$anonymous$$.. Greetz, $$anonymous$$y.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Click and move interface 1 Answer

Why doesn't my script work? 1 Answer

Why isn't this script working? (Beginner in coding) 2 Answers

switching gun on screen with gun you pick up. 3 Answers

Wrapping A Game Map.. 4 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