Have an object stop raycasting?

If you have seen my previous questions, I am using a very roundabout way to have an object stop raycasting which is to replace the object itself before it can generate millions of itself and crash Unity. This has proven not to work. A simple way I suppose is to see if the raycast is hitting “insert string name” and then change the string, but that doesn’t seem to work either. Has anyone had this problem or a variation of this problem? Any help is appreciated.

var spaces : int = 0;

var Cube : Transform;
var Empty : Transform;

var generate : boolean = false;
var visable : 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)){
    
    if (hit.collider.gameObject.name == objectName)
    {
        generate = true;
        objectName = null;
    }
    }
    
    if (spaces == 2)
    {
    generateFloor();
    spaces = 0;
    }

    //********************************
    //Generate chances
    //********************************

    if (generate == true)
    {
        chances = Random.Range (0,101); 

        if (chances >= 40) 
        {
            generateFloor();
        }
        else if (chances >= 60) 
        {
            generateDrop();
            spaces++;
        }
        
        Destroy(gameObject);                     
    }
}
 
//********************************
//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 generateDrop() 
{
    var newCube = Instantiate (Empty, transform.position + Vector3(startSize, 0, 0), Quaternion.identity);
}

//********************************//

function OnBecamevisible () 
{
    visable = true;
}

function OnBecameInvisible () 
{
    Destroy(gameObject);   
}

I guess that your problem is related to two things - the fact that you don’t set generate to false and the instantiate/destroy issue I linked in an answer to your previous question.

To solve you problems, follow these steps:

  1. You have to create simple manager class, similar to the one from first of above links. In JavaScript simple CubeManager script can look like this:

    class CubeManager extends MonoBehaviour
    {
    var Cube : Transform;
    var platformCube : Transform;
    var Empty : Transform;
    var emptyCube : Transform;

     private static var instance : CubeManager = null;
    
     static function GetInstance()
     {
     	if (instance == null)
     	{
     		instance = FindObjectOfType(typeof(CubeManager));
     	}
     	return instance;
     }
    

    }

  2. Create an empty game object, and attach CubeManager to it.

  3. Initialize all the CubeManager cube variables in the Inspector with your prefabs.

  4. Remove all cube variables from your generation script.

  5. Change Update method in your generation script:

    function Update ()
    {
    transform.Translate(Vector3.left * movementSpeed, Space.World);

     if(shouldMoveOnly) // this must be additional global boolean variable, initiall set to false
     {
     	return;
     }
    
     var up = transform.TransformDirection(Vector3.up);
     var hit : RaycastHit;
     if (Physics.Raycast(transform.position, up, hit))
     {
     	if (hit.collider.gameObject.name == objectName)
     	{
     		Debug.Log("hit");
     		generate = true;
     		shouldMoveOnly = true;
     	}
     }
    
     // rest of your code
    

    }

  6. Remove DestroyObject at the end of Update.

  7. Change Instantiate methods calls to use CubeManager. For example:

    Instantiate(platformCube, position, rotation)

to:

Instantiate(CubeManager.GetInstance().platformCube, position, rotation)

I tested in on my machine, and it works ok.

And don’t forget to read two above links. They are crucial for you to understand why initializing prefabs without such additional manager is not a good idea.