How to check if an object has collided from another script?

public bool touchCheck;
private bool self;
private bool notSelf;

void OnTriggerEnter(Collider other) {

	if (other.gameObject.tag == "Piece") {

		if (other.transform == gameObject.transform.parent) {
			self = true;
		} else if (other.transform != gameObject.transform.parent) {
			notSelf = true;
		}

		if (self && notSelf) {
			touchCheck = true;
			Debug.Log ("Pieces touch!");
		}
	}
}

The above is the “TouchCheck” script attached to a child object, checking if it collides with it’s parent and another object at the same time. This script works. My problem is this: I want a separate script “RoomGen” to tell me what “TouchChecks” value is. When I get to the point in my “RoomGen” script that asks whether there is a collision before it spawns another piece, it’s value will NEVER return true despite the “TouchCheck” collision script saying there has been a collision.

I would appreciate any help as I haven’t been able to figure this out for two days.

public GameObject Piece01, Piece02, Piece03, Piece04, Piece05, Piece06, Piece07, Piece08, Piece09; 
private GameObject nextSpawnObj;
private GameObject spawnObject;
GameObject touchCheckObj;

public int maxPieces;
int spawnCount;
int rndPieceSpawn;
Vector3 nextSpawnPos;
Quaternion nextSpawnRotation;

void Start () {
	
	nextSpawnPos = Vector3.zero;
	nextSpawnRotation = new Quaternion();
}

void Update () {

	if (spawnCount != maxPieces) {
		
		rndPieceSpawn = Random.Range (1, 10);

		switch (rndPieceSpawn) {
		case 1:
			spawnObject = Piece01;
			break;
		case 2:
			spawnObject = Piece02;
			break;
		case 3:
			spawnObject = Piece03;
			break;
		case 4:
			spawnObject = Piece04;
			break;
		case 5:
			spawnObject = Piece05;
			break;
		case 6:
			spawnObject = Piece06;
			break;
		case 7:
			spawnObject = Piece07;
			break;
		case 8:
			spawnObject = Piece08;
			break;
		case 9:
			spawnObject = Piece09;
			break;
		}

		SpawnObj (spawnObject);

		touchCheckObj = GameObject.Find("touchCheck");
		TouchCheck touchCheckScript = touchCheckObj.GetComponent<TouchCheck>();
		bool check = touchCheckScript.touchCheck;
			
		if (check == true) {
			Debug.Log ("Pieces touch! - RoomGen");
		}

		if (check == false) { 
		FindNewSpawnPos ("nextObjSpawnPoint");
		FindNewSpawnRota ("nextObjSpawnPoint");
		Destroy (touchCheckObj.gameObject);
		spawnCount += 1;
	}
			
}

}

private void SpawnObj(GameObject Obj) {

	GameObject.Instantiate (Obj,nextSpawnPos, nextSpawnRotation);
}

public void FindNewSpawnPos (string nextObjSpawnPoint) {
	
	nextSpawnObj = GameObject.Find (nextObjSpawnPoint);
	nextSpawnPos = nextSpawnObj.transform.position;
}

public void FindNewSpawnRota (string nextObjSpawnPoint) {

	nextSpawnObj = GameObject.Find (nextObjSpawnPoint);
	nextSpawnRotation = nextSpawnObj.transform.rotation;
	Destroy (nextSpawnObj);
}

In your RoomGen, create a TouchCheck object, something like this.

TouchCheck check;

void Start() {

     // Sets check object to touchCheck boolean 
     check = GameObject<TouchCheck>().touchCheck;
}

void Update() {
     // Use touchCheck boolean
     if (touchCheck) {
          
          // Do this if touchCheck is TRUE
     } else {

          // Do this instead if touchCheck is FALSE
     }
}

I don’t know if this is most optimal, but this is what I do usually. Let me know if that works! :slight_smile: