- Home /
Instantiate GameObject Parent targetting issue.
I'm working on a script that targets an enemy, and when said enemy is targeted it Instantiate a GameObject that will display health and such.
Instantiating and spawning it ON a parent object works fine, however it won't spawn as a child (and therefore won't travel with the target as it moves.) I've got other scripts that spawn in the same fashion, so I'm not sure on the issue.
Also getting an error of NullReferenceException: Object reference not set to an instance of an object issue.
private void TargetEnemy() {
if(selectedTarget == null) {
SortTargetsByDistance();
selectedTarget = targets[0];
}
else {
int index = targets.IndexOf(selectedTarget);
if(index < targets.Count - 1) {
index++;
}
else {
index = 0;
}
DeselectTarget();
selectedTarget = targets[index];
}
SelectTarget();
}
private void SelectTarget() {
GameObject targetHUD = Instantiate(TargetHUD, selectedTarget.position, selectedTarget.rotation) as GameObject;
targetHUD.transform.parent = selectedTarget;
}
private void DeselectTarget() {
Destroy (GameObject.FindWithTag("TargetHUD"));
selectedTarget = null;
}
Where does the Null Reference Exception occur? What line?
Your answer
Follow this Question
Related Questions
Renaming Child Objects during Instantiate? 1 Answer
Instantiated GameObject gets spawned as a child 2 Answers
Instantiate prefab as Child to remove and re-do later 2 Answers
Instantiated GameObject collision without script repetition? 1 Answer
Creating new Transform from existing objects Transform to Instantiate object 1 Answer