- Home /
Homing Missile target with "Find Objects With Tag" not working?
I'm trying to make a homing missile. The way I have the spells in my game, when you press the fire button, it instantiates a prefab. That prefab then does whatever the spell or ability is supposed to do, and then destroys itself. Each prefab is generic and every player casts the same prefabs. The prefabs are also fired from a "Gun" null object that's parented to the player, so there's a slight disconnect in the hierarchy. To help with that, the "Cast" button creates each prefab as a child of the "Gun" null object.
Hopefully that makes sense. The reason that's all relevant, is because when this homing missile is fired, it has to find a target. It sifts through the "Find Objects with tag" code that unity script reference offers, and SHOULD then assign the "closest" to the "target". But it doesn't. I've tested this code with a hard-coded target, and it works just fine. I've tested it with a more basic "just find anything with the player tag" and it works there too. It's just the script reference "FindClosestPlayer" function that doesn't seem to be working.
Or maybe I'm just trying to interact with it the wrong way. Either way, I keep getting the following error:
"NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs) Chase.Update () (at Assets/Other/Spells/Chase.js:23)"
And I'm stumped at this point. Please peruse my code and tell me exactly how and why I am an illiterate moron.
var target : GameObject; //the missile's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var lifeTime : float;
var myTransform : Transform; //current transform data of this enemy
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
FindClosestPlayer();
Destroy(gameObject, lifeTime);
}
function Update () {
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
// FIND THE CLOSEST PLAYER OTHER THAN THE CURRENT PLAYER
function FindClosestPlayer() : GameObject {
// Find all game objects with tag Player
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("Player");
var distance = Mathf.Infinity;
var position = transform.position;
// Iterate through them and find the closest one
for (var players : GameObject in gos) {
if (players == gameObject.transform.root) continue;
var diff = (players.transform.position - position);
var curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closestPlayer = players;
distance = curDistance;
}
}
return closestPlayer;
target = closestPlayer;
}
Answer by Griffo · Apr 22, 2014 at 06:34 AM
is it because you return before setting target = closestPlayer;
should it not be ..
target = closestPlayer;
return closestPlayer;
also add
target = FindClosestPlayer();
in Start()
There are two errors beside the above in the code line 22 should be
Quaternion.LookRotation(target.transform.position - myTransform.position), rotationSpeed*Time.deltaTime);
and line 44 var has not been declared, so need to add to the top
var closestPlayer : GameObject;
Then it should work.
Woo! That worked!
$$anonymous$$y exclusion code isn't working properly, line 40 if (players == gameObject.transform.root) continue;
Do you have any brilliant solutions for excluding anything along the hierarchy of the prefab? (Sorry to ask this one, I'm still new at this and I haven't found any examples of this sort of thing online)
You could do something like
if (players.tag == gameObject.transform.root.tag) continue;
so it will only target the Player.
Not sure what you'r trying to do? you say "exclude anything along the hierarchy of the prefab" if you'r firing a missile at the GameObject "Player" then anything child of the Player is going to get destroyed if you destroy the GameObject Player.
It's more that I have two players. Each with the same "player" tag. I want either one to be able to shoot the missile and have it target the other, but not itself.
Why don't you tag them as "Player_01" and "Player_02" then change line 40 to
if (players.tag != transform.root.tag) continue;
it'd work for now, but I'd eventually like to have things affect all players equally. And after playtesting, my friends are begging me to make it a 4 player game, so I'd like it to just have one tag to sift through. Find a player. Is it the nearest player, but not the player who cast this spell? ok, go after that player.
Answer by getyour411 · Apr 22, 2014 at 05:21 AM
You only call FindClosestPlayer once in Start(), so if another player isn't found at Start(), closestPlayer is probably null which is why 23 breaks down. You'll need to move the call to FindClosestPlayer in a coroutine, invoke, update, etc. and you should test that closestPlayer is not null
I may just be tired from staring at this all day, but would it work if I set up in Update so that "if (target != null}" then it just runs "FindClosestPlayer ()" again?
Your answer
![](https://koobas.hobune.stream/wayback/20220613143306im_/https://answers.unity.com/themes/thub/images/avi.jpg)