- Home /
Help with script (limiting gameobject but when destroyed allowing again)
Hi all, i'm a complete newbie to Unity and trying to get to grips with it. I can't code to save my life (at the moment).
I have made a breakout style game (WIP) and I have the following issue I can't get past:
I am using this shooting script (from another post on here) to fire my "Bullet" (ball in game):
var thePrefab : GameObject;
var MaxObjectCount = 0;
function Update () {
if(Input.GetButtonDown("Jump")){
var instance : GameObject = Instantiate(thePrefab, transform.position, transform.rotation);
instance.name = "Bullet";
MaxObjectCount += 1;
checkhit();
}
}
function checkhit(){
if(MaxObjectCount == 2){
Destroy(GameObject.Find("Bullet"));
MaxObjectCount -=1;
}
}
this kinda works (not ideal) to let the player ONLY fire 1 ball (which is what I wanted). Onto my problem:
Using this script (on my floor) destroys the ball when the player misses and it hits the floor:
function OnCollisionEnter ( collision : Collision) {
if (collision.collider.name == "Bullet")
Destroy (GameObject.Find("Bullet"));
}
The problem I have is that once the ball is destroyed for the first time (after hitting the floor) I can't fire any more at all. I don't know what to add to the shooting script to make it allow the player to fire again (just one ball at a time again)
any help would be appriciated greatly!
Answer by immersivegamer · Mar 01, 2012 at 06:40 AM
Ok, that was a big pain. Have to rewrite everything again. At least I know what I'm going to be writing.
Haven't tested your scripts out, but they are fine. The problem is in the logic of the code. In the update() function, when ever a second bullet is created it adds to the var MaxObjectCount, however when it gets to 2 then in the checkhit() function, the last object to be created will be destroyed. Now here is the important part, in the check()after destroying the object they decrease the value of MaxObjectCount by one. Your collision check on the floor does not do that. So what happens is that when the bullet hits the floor MaxObjectCount become 2. This means that when you try to fire the next time check() will be called and destroy the object imdeiatlly. At least that is how I understand it.
So what you need to do is to update the variable from the collision check function.
//This is the object that has the shoot script on it. var shootObject : GameObject;
function OnCollisionEnter ( collision : Collision) {
if (collision.collider.name == "Bullet"){
//replace both 'ScriptName' with the name of the script that has your code in it.
var shootScript : ScriptName = shootObject.GetComponent(ScriptName);
Destroy (GameObject.Find("Bullet"));
//modifies the var.
shootScript.MaxObjectCount -= 1;
} }
Hopefully I didn't mess it up this time around. Someone will have to speak up if I forgot something.
As I said haven't tested anything, though that should fix your problem. If it doesn't then I'll test out all the scripts when I have the time.
Thank you very much immersivegamer that works perfectly :D
I never even considered my floor colision script, makes sense now (at least the logic of it, the code still makes my headspin until I can learn it (hopefully)
Glad that it worked, was worried that I might have interpreted the code wrong when I was looking at without testing it. Also sorry if that answer looked confusing, seems like my first answer got merged with the second, I'll edit it now.
@Carter: If the answer immesivegamer gave you was correct, you should acknowledge this by accepting his answer.
@kleptomaniac - thanks, I wasn't aware I had to do that :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613064628im_/https://answers.unity.com/themes/thub/images/avi.jpg)