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!

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.