- Home /
Problem is not reproducible or outdated
Is this the correct way to handle autonomous creeps?
So I mostly want to know if I am even in the right realm for a MoBA-like game where waves of creeps spawn. I have the spawning down fine, but the creep AI is giving me problems. I have a creep script that mostly handles everything. Also on the creep object, I also have a projectile shooter script, and a mover script. I also have a projectile script on my projectile prefab that handles destroying the gameobject when it collides with certain things. One specific problem I'm having is a nullreferenceexception on this line:
Creep unknownObject = col.GetComponentInParent<Creep>();
and all the others that handle teamchecking. What can I do to help, and am I even on the right track? Thanks
Here is the Creep Script which is throwing the nullref public class Creep : MonoBehaviour {
public float selfHealth;
public float damagePossible;
public int selfTeam=0;
public Transform enemyBase;
public Transform target;
public List<Transform> targetQueue;
[HideInInspector]
public bool canAttack;
[HideInInspector]
public bool canAttackBase;
[HideInInspector]
public bool emptyQueue;
//private bool targetChanged;
private bool canMove;
private bool queryBase;
private bool queryCreep;
private Creep _creep;
private Mover _mover;
private GameObject _thisInstance;
private ProjectileShooter _pShooter;
public virtual void Awake()
{
Init();
}
public virtual void Init()
{
targetQueue = new List<Transform>();
//canMove = true;
canAttack = false;
canAttackBase = false;
//targetChanged = false;
emptyQueue = true;
_creep = GetComponent<Creep>();
_mover = GetComponent<Mover>();
_thisInstance = gameObject;
_pShooter = GetComponent<ProjectileShooter>();
}
public void SenseThenAct(Collider col)
{
tag = col.transform.tag;
switch (tag)
{
case "Creep":
Creep unknownObject = col.GetComponentInParent<Creep>();
Debug.Log("!!Object In Trigger Is Creep");
if (unknownObject.selfTeam != selfTeam)
{
canAttack = true;
if (target != null)
{
AddToTargetQueue(col.transform);
break;
}
else
{
MakeActiveTarget(col.transform);
break;
}
}
else
{
break;
}
case "Base":
Headquarters unknownHQ = col.GetComponent<Headquarters>();
Debug.Log("!!Object In Trigger Is HQ");
if (unknownHQ.selfTeam != _creep.selfTeam)
{
if (target != null)
{
canAttackBase = true;
break;
}
else
{
MakeActiveTarget(col.transform);
break;
}
}
else
{
break;
}
case "Projectile":
Debug.Log("!!Object In Trigger Is Projectile");
Projectile unknownProjectile = col.GetComponent<Projectile>();
if (unknownProjectile.parentTeam != _creep.selfTeam)
{
ApplyDamage(unknownProjectile.Damage);
break;
}
else
{
break;
}
default:
break;
}
}
void MakeActiveTarget (Transform trans)
{
_pShooter = gameObject.GetComponent<ProjectileShooter>();
target = trans.transform;
//targetChanged = true;
}
void AddToTargetQueue(Transform trans)
{
targetQueue.Add(trans.transform);
emptyQueue = false;
}
void MakePrimaryTargetFromQueue()
{
target = targetQueue[0];
targetQueue.RemoveAt(0);
}
public void OnTriggerEnter(Collider col)
{
SenseThenAct(col);
}
public void OnTriggerExit(Collider col)
{
if (col == target)
{
if (targetQueue.Count == 0)
{
target = enemyBase.transform;
emptyQueue = true;
}
else if (targetQueue.Count > 0)
{
target = targetQueue[0];
targetQueue.RemoveAt(0);
if (targetQueue.Count == 0)
{
emptyQueue = true;
}
}
}
else if (col == enemyBase)
{
// THIS IS WHERE GAMEOVER SCRIPT SHOULD BE CALLED
}
}
public void HaltAttack()
{
target = null;
canAttack = false;
canAttackBase = false;
emptyQueue = true;
_mover.ResumeMovement();
}
protected virtual void ApplyDamage(float damageIncoming)
{
if (_creep.selfHealth - damageIncoming <= 0)
{
DestroyInstance();
}
_creep.selfHealth -= damageIncoming;
}
protected void DestroyInstance()
{
Destroy(_thisInstance);
}
}
Answer by tormentoarmagedoom · Oct 09, 2017 at 08:46 AM
I don't know if is the real problem, but in :
Creep unknownObject = col.GetComponentInParent<Creep>();
I should do it more step by step to see to be sure by looking first for the parent-gameobject and then the component. col, is a collider, and the parenting chain is from transform to transform.
Creep unknownObject = col.gameObject.transform.parent.gameobject.GetComponent<Creep>();
Ah! Of course. $$anonymous$$y scripts are attached to an empty gameObject, and my collider is the child. Thanks!
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Keeping the int value "min" to be used in another scene. 2 Answers
render sprite inside of a mask and outside of another mask 0 Answers