[TowerDefense] enemies dont pass other enemies in combat [solved]

hi i’m working on a tower defense and my enemies need to pass other fighting enemies. but they pile up instead. i need a reflection/code review on what i did. i cant seem to find the problem
issues : enemies

  • NullReferenceException: Object reference not set to an instance of an object. on line 76 and 91
  • ants dont pass each other they just pile up

 using UnityEngine;
    using System.Collections;
    
    public class antBehaviour : MonoBehaviour {
    
    	public float hitPoints = 50;
    	public float resistance = 0;
    	public float walkSpeed =2;
    	public string status = "follow_route";
    	public GameObject route;
    	public float Strength = 20f;
    	public float attackSpeed= 1f;
    	public bool invisible = false;
    	public bool builder = false;
    	public bool redBlue = false;
    
    	public GameObject target;
    
    	private int ignore = 0;
    	// Use this for initialization
    	void Start () 
    	{
    	
    	}
    	
    	// Update is called once per frame
    	void Update () 
    	{
    		MoveTo MT = gameObject.GetComponent<MoveTo> ();
    		ignore --;
    	
    
    		if (hitPoints <= 0) 
    		{
    			Destroy(gameObject);
    		}
    
    		if (status == "follow_route") 
    		{
    			if (MT.enabled == false) 
    			{
    				MT.enabled = true; 
    			}
    		}
    
    
    		if (status == "in_Conbat") 
    		{
    
    			if (MT.enabled ==true)
    			{
    				MT.enabled = false; 
    			}
    			if (target == null)
    			{
    				status = "follow_route";
    			} else
    			{
    			float step = walkSpeed * Time.deltaTime;
    			transform.position = Vector3.MoveTowards(transform.position, target.transform.position, step);
    			}
    		}
    
    	}
    
    
    	void OnTriggerEnter(Collider other)
    
    	{
    		//Debug.Log ("other "+ other);
    		antBehaviour OAB= other.gameObject.GetComponent<antBehaviour>(); 
    		MoveTo MT = gameObject.GetComponent<MoveTo> ();
    
    		if (redBlue ==true)
    		{
    			if (OAB.status == "in_Conbat")
    			{	
    				ignore = 50;
    				Debug.Log("ignore");
    			}else	if (OAB.status == "follow_route" && other.tag == "EnemyB" && ignore <= 0  ) 
    			{
    				target = other.gameObject;
    				status = "in conbat";
    				MT.enabled = false; 
    				OAB.status = "in_Conbat";
    				OAB.target = gameObject;
    			}
    		}
    		if (redBlue ==false)
    		{
    			if (OAB.status == "in_Conbat")
    			{
    				ignore = 50;
    				Debug.Log("ignore");
    			}else	if (OAB.status == "follow_route" && other.tag == "EnemyR"&& ignore <= 0   ) 
    			{
    				target = other.gameObject;
    				status = "in conbat";
    				MT.enabled = false; 
    				OAB.status = "in_Conbat";
    				OAB.target = gameObject;
    			}
    		}
    
    	}
    }

You are setting the status as ‘in conbat’ and ‘in_Conbat’ for the same script attached on different objects so there will be conflict when script from one object accesses another script.

Consider for ant A you set status as ‘in conbat’ for ant A’s script and as ‘in_Conbat’ for ant B’s script from script of ant A. Now when the script from ant B tries to access your ant A’s script it will check for status to be ‘in_Combat’ but it is actually ‘in conbat’.

I think you get the point.

A rather better way to handle this type of situations is to use enum when you have limited number of states.

I’m just not clear with your ‘ants dont pass each other they just pile up’ question. Can you elaborate on this in more detail?