- Home /
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?
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.
Ok that fixed it thanks, umm if I have another question do I create a new question? or do I continue form here
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.
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.
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.
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
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.
I tried it without the other script, it still shoots the ball and the Debug.Log outputs with TRUE even when it is shooting
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
Follow this Question
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