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);
}