Best way of interection between scripts.

i try to figure out following situation. Player enter the building, enemy noticed this action and follows the player. Question is how should enemy script know that player made a collision with building. How can script get acces to collisons functions of anouther object? sorry for mistakes, i am typing from my cellphone.

3 Answers

3

place a box collider in the entrance.

public class Entrance: MonoBehaviour 
{
public GameObject[] enemies;
public GameObject player;
	
public void OnTriggerEnter(Collider other)
	{
	if(other.transform.gameObject != player) return;

    foreach(GameObject e in enemies)
       e.target = player;
	}
}

in your Enemy AI Script check if the enemy has a target.
You can also replace the player with a playerTag string.

Theres multiple ways of implementing this, but i think the best would be putting the code to alert the enemies within a collider (or better yet, trigger) that outlines the building. If theres only one enemy, its fairly straightforward. In C#:

void OnTriggerEnter (Collider other) {
	if(other.gameObject == player) { 		// identify player somehow, either by tag or some other way.
		enemy.GetComponenet<EnemyScript>().alerted = true;		// identify enemy in a similar fashion. 
	}
} 

This is the most straightforward way i can think of. EnemyScript is whatever script you have on your bad guy, and it would have a boolean value that would change when the collision is made. I think the best way is to put this code on an empty object with a collider that outlines the entire building. Make sure its marked as a trigger.

If you have multiple enemies, the code would change slightly. Again in C#:

void OnTriggerEnter (Collider other) {
	if(other.gameObject == player) { 
		foreach (GameObject enemy in GameObject.FindGameObjectsWithTag("Enemy")){
			enemy.GetComponenet<EnemyScript>().alerted = true;
		}
	}
}

The FindGameObjectsWithTag returns an array of all the objects tagged “Enemy.” Obviously you’d need to make sure they’re tagged, or repeat this code multiple times if you have more than one tag for enemies. If your coding in Unity(java) Script, the syntax is different, but the idea is the same.

I hope this helps you out a bit, and let me know if you have questions.

that always happens to me...

there is no best way, it always depends on the problem domain. In this particular case, you can create another “alert system” script which notifies enemies if triggered (see Observer Pattern).

Example: You have several instance of an alert component (lets name it Alert). Each alert component has a field that links to it’s system (AlertSystem) and if the player triggers an alert, it will tell the alert system to start ringing. Enemies can listen to an alert system and start acting if it goes off (the system will tell its subscribers: go hunting).
Of course, enemies must first subscribe to the alert system and the alert system must find all its child alerts, but this easy to do on start up where you use GameObject.Find and GetComponent to find and link all behaviours

Yes, you are right. Thanks kabel and djmorrsee for great samples and main ideas, really helps, and I need to think of "AlertSystem", thanks japanitrat.