Returning Values from custom namespaces C#
Hey guys, this is a bit of a throwback to a script that I had asked about maybe a week ago, so apologies if this seems repetitive.
I have a number of different scripts that, because of a variety of reasons, all tend to use identical or nearly identical search functions of game objects (Find closest target of specific type, Find target with lowest health of specific, find random target of specific type, etc). The end result is that I have an increasing number of scripts that are starting to run essentially identical functions within them. Rather than copying and pasting these functions, I considered it'd be far more efficient (and easier on the eyes) to just pool these functions in a custom namespace so that I could access them easier and with less lines of code.
Here's an example of the code(s) I'm working with.
(Master Script containing the functions)
namespace MasterScript
{
public class FindClosestTarget : MonoBehaviour
{
public static GameObject findclosesttarget(string a, Transform thisobject)
{
GameObject[] gos = GameObject.FindGameObjectsWithTag(a);
float distance = Mathf.Infinity;
Vector3 position = thisobject.transform.position;
foreach (GameObject go in gos)
{
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
if (go.GetComponent<Life>() != null)
{
distance = curDistance;
return go;
}
}
}
return null;
}
}
}
And here is an example of trying to call the function in which I am trying to get a target found:
void Update () {
target = null;
if (this.tag == "User")
{
target = FindClosestTarget.findclosesttarget("Enemy", this.transform);
move();
}
if (this.tag == "Enemy")
{
target = FindClosestTarget.findclosesttarget("User", this.transform);
move();
}
}
The Good news is that every time, a target is returned.
The PROBLEM is that when I have this script running on multiple units, they all return the SAME target of their respective designation. (ex All guys with "User" focus on ONE target with "Enemy" at all times regardless of other conditions)
My hunch is that I am supposed to do something along the lines of like "FindClosestTarget targ = new FindClosestTarget" or something within that area, but I haven't had much luck in that regards.
Any help is greatly appreciated.
***UPDATE
I've isolated the problem somewhere in this area on script 1
{
GameObject[] gos = GameObject.FindGameObjectsWithTag(a);
foreach (GameObject go in gos)
{
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
if (go.GetComponent<Life>() != null)
{
distance = curDistance;
return go;
}
}
}
}
As gos seems to only be grabbing 1 object rather than an array of the entire screen
Answer by tag104367 · Oct 30, 2016 at 09:02 PM
So you won't need to call a new FindClosestTarget()
anywhere seeing how your function is static doing new won't do anything. Not sure if this is the complete solution but it looks like you are passing in a transform to the function using this.transform
(lines 6, 11 of second script) and then you are using the thisobject.transform.position
in your first script. Maybe try using thisobject.position
instead of thisobject.transform.position
I changed thisobject.transform.position, unfortunately it still yields the same results in that everyone still pools to one target :\