Question by
Caruzo · Apr 15, 2017 at 02:27 PM ·
if-statementsvoidstatement
Void still running even when if statement is not fulfilled?
Hey guys,
I have an issue where my operation keeps executing even the if statement is not fulfilled.
public EnemyDetection enemyDetection;
public LayerMask defaultLayer;
private NavMeshAgent agent;
// Use this for initialization
void Start () {
agent = GetComponent<NavMeshAgent> ();
determinedWaypoint = false;
laneToFollow = 10;
enemyDetection = GetComponent<EnemyDetection> ();
}
void walk(){
if (enemyDetection.enemiesInRangeFound.Length <= 0) {
agent.destination = targetWayPoint.position;
}
}
while this script updates the enemiesInRangeFound array:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyDetection : MonoBehaviour {
private Transform myTransform;
public float radiusOfDetection;
public LayerMask layerToDetect;
private WaypointMoving waypingMoving;
public Collider[] enemiesInRangeFound;
// Use this for initialization
void Start () {
radiusOfDetection = 25;
StartCoroutine (waitToLayer ());
}
// Update is called once per frame
void Update () {
DetectEnemies ();
}
void DetectEnemies () {
enemiesInRangeFound = Physics.OverlapSphere (transform.position, radiusOfDetection, layerToDetect);
}
IEnumerator waitToLayer () {
yield return new WaitForSeconds (0.5f);
if (gameObject.layer == 15) {
layerToDetect = 1 << 17;
}
}
}
Even though I have a debug that shows me the length of the enemiesInRangeFound gets higher than 0, the MOB still keeps moving (agent.destination = targetwaypoint.position).
This was working before I tweaked the script to be useful for navmeshagent.
Any idea why this is still triggering?
Comment
Your answer
Follow this Question
Related Questions
How to destroy something after X hits 2 Answers
The bool activates and deactivates but the script in the if statement doesn´t work 1 Answer
Cant get an if statement to activate even if the statement is fulfilled. 0 Answers
mathf.abs(0) is higher than 90, why? 1 Answer
Help with rotation please 2 Answers