- Home /
How can I tell my laser which enemy to target?
So this is my problem. It is easy to track position of "specific" gameObject but I have problem while tracking when I have same gameObject in the scene ---> I Instantiante couple enemyes and each one of them Intantiate their own laser who is tracking position of that gameObject...
PROBLEM : if I have one enemy it is fine and laser is folowing position of game object but when having more of them lasers do not know which position they should track becouse there is more than one enemy on the screen.
I hope I explained everything fine and thanks for help.
Create new parameter ("ID", for example) in gameObject and laser In spawner object before instantiates new object set each time new ID (just increment it). Then in laser script check ID of laser and gameObject, if it equals - track, if not - dont track.
// gameObj script
public var ID : int;
// ur code here
// spawner object script
public var Prefab : GameObject;
public var PrefScr : NameOfPrefabScript; // name of script with id attached to prefab
public var action : bool; // trigger
public var ID : int;
function Start () {
PrefScr = Prefab.GetComponent(NameOfPrefabScript);
function Update () {
if (action){ // if trigger set on
PrefScr.ID = ID;
Instantiate (Prefab, transform.position, transform.rotation);
ID++;
action = false; // set trigger off
}
}
// laser object script. i dont know how u create laser for new game objects, get ID like in gameObj/spawner for this too.
public var ID : int;
public var parent : GameObject;
public var parentScr : NameOfPrefabScript;
function Start () {
parentScr = parent.GetComponent(NameOfPrefabScript);
function Update () {
if (ID == parentScr.ID){
// do what u want, track position and etc.
}
}
Answer by Ben-Stoneman · Feb 16, 2015 at 01:52 PM
You need a way of distinguishing the enemy you need to target.
This question is not a general question on how do I track a GameObject, but instead the question is "How can I tell my laser which enemy to target?". The answer of course is up to your designer it depends on what enemy you want too target, the one closest?
Thanks guys, I'm using c# but logic shouldn't be that different but yea I can see what you were going for. Thanks again.