Stop instantiate on collision from other script

Hey guys,

This question comes in completion of my previous one “How to limit instantion of prefabs/gameobjects”.

So I have a script called “CreateWall” that I use to instantiate objects in certain directions on mouse click&drag.

The instantiation code looks something like this:

function SpawnFence()
{

if (sss == 1 && !someCol.someBool)
		{
			position_var = clone2.position + Vector3(0,20,0);	
			clone3 = Instantiate(wallObject, position_var, Quaternion.identity);
			clone3.parent = parentofwall;
			clone3.transform.position.z = 0;
			last_clone = clone3;
			
			if(!someCol.someBool){
			sss = 2;
			}
		}

and it goes on and one until the last clone.

The collision script is currently set on the object that is being instantiated and it has the following code:

function Awake()
{
	wallCreate = GameObject.Find("Background - GameManager").GetComponent.<CreateWall>();
	someBool = false;
}

function OnTriggerEnter(hit:Collider)
{
	if(hit.collider.tag == "SomWall")
	{
		wallCreate.sss = 0;
		Debug.Log ("collided");	
		someBool = true;	
	}
}

Here’s my problem. The sss var is being set to 0 in my CreateWall, my someBool is becoming true right when colliding but i think objects are instantiating too fast for the script to have time to run through this. Should I slow it down using WaitForSeconds or should I rewrite my whole script?

SEE LAST COMMENT FOR MORE INFO ON THE FIX!

A few things are noticeable:

First, it would always be easier for you (and for us to understand) if you give the variables more meaningful names.

Now, as for the essence of this question - I would assume your problem is that the sss variable is being controlled by both an outside source (your collision detection) and the condition itself, which I can assume is in a loop.

So, as a first test, separate the two. Let the collider control a different variable that is untouched by anyone other then the collider itself ( like wallCreate.isCreationEnabled = false ).

If I am reading your code correctly, I have a feeling this approach will point you in the right direction.