Script 1
public class enemy : MonoBehaviour {
private Vector3 Player;
private Vector2 PlayerDirection;
public float Xdif;
public float Ydif;
private float speed;
private float distance;
private bool stun;
private float stuntime;
GameObject playerObj;
private Rigidbody2D myRigidbody2D;
private bool isEnemyFear;
// Use this for initialization
void Start () {
stuntime = 0;
stun = false;
speed = 6;
playerObj = GameObject.Find ("Player");
myRigidbody2D = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
public void Update () {
distance = Vector2.Distance (Player, transform.position);
Player = playerObj.transform.position;
if (stuntime > 0) {
stuntime -=Time.deltaTime;
} else {
stun = false;
}
if(isEnemyFear){ //here to pass a value from script 2
EnemyFear();
}
else{
EnemyAttack();
}
}
void EnemyAttack(){
if (distance < 25 & !stun) {
Xdif = Player.x - transform.position.x;
Ydif = Player.y - transform.position.y;
PlayerDirection = new Vector2 (Xdif, Ydif);
myRigidbody2D.AddForce (PlayerDirection.normalized * speed);
Debug.Log("Attack");
}
}
void EnemyFear(){
if (distance < 25 & !stun) {
Xdif = Player.x + transform.position.x;
Ydif = Player.y + transform.position.y;
PlayerDirection = new Vector2 (Xdif, Ydif);
myRigidbody2D.AddForce (PlayerDirection.normalized * speed);
Debug.Log("Fear");
}
StartCoroutine(Timer());
}
void OnCollisionEnter2D(Collision2D Playerhit){
if (Playerhit.gameObject.CompareTag("Player")) {
stun = true;
stuntime = 1;
}
}
IEnumerator Timer() {
yield return new WaitForSeconds(10);
isEnemyFear = false;
StopCoroutine ("Timer");
}
}
Script 2
public class randomizer : MonoBehaviour {
private bool isEnemyFear;
public void CheckFear(){
int rand = Random.Range(0, 100);
if(rand > 35)
isEnemyFear = true; //this value should be passed to the script 1
}
}
How to pass a value isEnemyFear = true from Script 2 in Script 1?