How do I store a list in an object consisting of ALL towers that it is in range of, and access that list from the towers?
I want to know which towers my little dudes have triggered OnTriggerEnter() for and save them in a little list (on the instance itself). I then want to use this data so my towers know if they can shoot said unit or not.
I'm having an issue currently where my towers lock on and fire onto a unit, but when he moves out of range he's still being shot because other instances of that unit are stiller triggering OnTriggerEvent/OnTriggerStay... globally?
Can someone show me how to code this? Also, if this isn't the best way to do this I'd be more than happy to hear other ways to code it.
Turret code follows. Check out lines 15, 25 and 38:
void Update()
{
// Find cloest player and lockon until it's dead, if it isn't already lockedon to another player or leaves range
if (!lockedon)
{
targetplayer = FindClosestPlayer();
lockedon = targetplayer;
}
// If lockedon to a target, attack
if (lockedon)
{
// If player is in range (OnTriggerEnter), attack
if (inrange)
{
// *****HOW DO I CHECK IF targetplayer's inrange VARIABLE == TRUE HERE INSTEAD?*****
Attack();
}
}
}
void OnTriggerStay(Collider co)
{
if (co.tag =="Player")
{
//***** HOW DO I SET THE SPECIFIC PLAYER INSTANCE inrange VARIABLE TO TRUE HERE INSTEAD?*****//
inrange = true;
Debug.Log(targetplayer);
}
}
void OnTriggerExit(Collider co)
{
// If it uncollides with an Enemy, set inrange false
if (co.tag == "Player")
{
//***** HOW DO I SET THE SPECIFIC PLAYER INSTANCE inrange VARIABLE TO False HERE INSTEAD?*****//
inrange = false;
Debug.Log("Uncollided with:");
Debug.Log(co.tag);
}
}
Answer by fgbg · Feb 25, 2017 at 09:12 PM
I fixed this after a bit of a headache. I now add or remove the gameobjects from the unitsinrange list when they trigger Enter or Exit. This is how I'll know if a unit is in range (and can therfore be shot). The tower's reference the list before attacking:
void Update()
{
// Find cloest player, lock on and attack until it's dead, as long as it is in range
Debug.Log("Does target player exist?" + targetplayer);
if (!targetplayer)
{
targetplayer = FindClosestPlayer();
Debug.Log("Closest target is: " + targetplayer);
}
// If locked on to a target, allow attack
else if(targetplayer)
{
// If player is in range, attack
Debug.Log("Is target in range? " + unitsinrange.Contains(targetplayer));
if (unitsinrange.Contains(targetplayer))
{
Debug.Log("Attacking: " + targetplayer);
Attack(targetplayer);
}
// If closest target wasn't in range in range, break the lock on
else
{
targetplayer = null;
}
}
}
void OnTriggerEnter(Collider co)
{
if (co.tag =="Player")
{
Debug.Log("Turret added this to list: " + co.gameObject);
unitsinrange.Add(co.gameObject);
foreach (GameObject unit in unitsinrange)
{
Debug.Log("Contents of list: " + unit);
}
}
}
void OnTriggerExit(Collider co)
{
if (co.tag == "Player")
{
unitsinrange.Remove(co.gameObject);
Debug.Log("Turret removed this from list: " + co.gameObject);
foreach (GameObject unit in unitsinrange)
{
Debug.Log("Contents of list: " + unit);
}
}
}
Your answer
Follow this Question
Related Questions
Access variable from different class 1 Answer
How do I specify an instance as a listener to an event trigger component? 0 Answers
How can I access material of a mesh renderer without receiving and instance of it? 1 Answer
Setting OnTriggerEnter to a second object collider 2 Answers
Collission is detected very late. onTriggerEnter is working very late (i.e 5 Seconds late) 1 Answer