Generating with scripts turned off?

Hey all,I have an odd situation. I have this script attached to a prefab that is spawned at the start and moves towards the player. When it hits the player, it raycasts and spawns another one of itself, or of a similar object.

My problem is, when it generates, half(?) the cubes behave as they should, but the other half starts with the generation and movement script off by default. I don’t even reference the script below. So eventually, the cubes just stop coming because they are all bunched up in one area.

I hate to pose long scripts, so if this is a bit too intimidating for a casual forum, please feel free to ask me to take it off. I will gladly do so. Thanks a bunch!

var visable : boolean = false;

var Cube : Transform;
var platformCube : Transform;
var Empty : Transform;
var emptyCube : Transform;

var chances : int;
var generate : boolean = false;

private var startSize : int = 25;
private var newPos : int = 0;
 
//********************************
//Raycast
//********************************
 
function Update () 
{

	//Move
    transform.Translate(Vector3.left * movementSpeed, Space.World);
    
    var up = transform.TransformDirection(Vector3.up);
    var hit : RaycastHit;  
 
    if (Physics.Raycast (transform.position, up, hit)) 
    {
       generate = true;
       Destroy(gameObject);
       generateEmpty();
    }
 
    //********************************
    //Generate chances
    //********************************  
 
    chances = Random.Range (1,101);
 
    if (generate == true) 
    {
    	generate = false;
    	
       if (chances <= 60) 
       {
         generateFloor();
       }
 
       if (chances <= 40) 
       {
         generateDrop();
       }
 
       if (chances <= 0) 
       {
         generatePlatform();
       }
    }
}
 
//********************************
//Generate level
//********************************  
 
function generateFloor () 
{
    var newCube = Instantiate (Cube, transform.position + Vector3(startSize, 0, 0), Quaternion.identity);
    newCube = Instantiate (Cube, transform.position + Vector3(startSize + 1, 0, 0), Quaternion.identity);
}
 
function generatePlatform() 
{
    var newplatformCube = Instantiate (platformCube, transform.position + Vector3(startSize, 3, 0), Quaternion.identity);
}
 
function generateDrop() 
{
    var newCube = Instantiate (Empty, transform.position + Vector3(startSize, 0, 0), Quaternion.identity);
}

function generateEmpty() 
{
	var newEmptyCube = Instantiate (emptyCube, transform.position + Vector3(newPos, 0, 0), Quaternion.identity);
}
 
 
//*********************************//
function OnBecamevisible () 
{
    visable = true;
}

function OnBecameInvisible () 
{
    Destroy(gameObject);

}

I’m glad that solution given by @-hiTo- works for you. I did a bit of research as well, and just for those wishing to learn a little more about similar problem and its cause, please take a look at this question.

When I was testing code provided by you, I’ve seen similar problem - script was disabled and names were longer and longer (Cube, then (Clone), then Cube(Clone)(Clone), etc.). After testing solution provided in linked question by @Bunny83, everything started to work as expected.

Still seeing a pretty ominous situation with that Destroy…

You want to keep running the function after calling destroy on that script. Which is weird. I would call Destroy at the end, only if generate is true.

Another thing: your percentages are off(?), like Jamora said.

But it’s more than what Jamora led on.

You are checking each and every if-statement.

Which means that if your value is 30, both if-statement #1 and if-statement #2 will run, and you will instantiate both a floor and a drop. You are also randomizing between 1 and 100, so your value can never be 0. Your last statement, the platform, will never run.

Here’s what I mean you should do instead:

function Update ()
{ 
    //Move
    transform.Translate(Vector3.left * movementSpeed, Space.World);
 
    var up = transform.TransformDirection(Vector3.up);
    var hit : RaycastHit;
 
    if (Physics.Raycast (transform.position, up, hit))
    {
        generate = true;
        generateEmpty();
    }
 
    //********************************
    //Generate chances
    //********************************

    if (generate == true)
    {
        chances = Random.Range (0,100); // No need to call a Random if we're not going to check the value. So move it here.
 
        if (chances >= 60) // 40 percent chance
        {
            generateFloor();
        }
        else if (chances >= 40) // 20 percent chance
        {
            generateDrop();
        }
        else // 100% - 40% - 20% = 40 percent chance
        {
            generatePlatform();
        }
        
        Destroy(gameObject); // It is unnecessary to set generate to false in here. 
                             //Because it will not run again, since we are destroying the object that this script runs on.
    }
}