Question by
BlackVandal · Jan 28, 2016 at 09:36 PM ·
javascriptgameobjecttransformtarget
Null Reference Exception for target in a script on a newly instantiated object
What I'm trying to do is spawn an enemy and create a marker/blip on the Minimap for the spawned enemy. I get a NullReferencException at line BlipCtrl:29.
The Blips on the Minimap have a Target GameObject which position reflects the position of the Blip on the Minimap. When an Enemy is instantiated I set it to have a unique name. I'm having trouble writing a function the sets the Target of the Blip to the GameObject with the Unique name that the Blip gets it's position and rotation from.
Spawner.js
public var enemy : Rigidbody ;
private var spawnValues : Vector3 = Vector3(50, 1, 50);
public var enemycount : int ;
public var enemyId : int;
static var enemiesarray = new Array();
public var blipcontroller : BlipCtrl;
var spawnPosition : Vector3;
var spawnRotation : Quaternion;
function Start () {
var blipcontroller = GetComponent("BlipCtrl");
SpawnWaves();
}
function SetName () {
enemy.name = "Enemy#" + enemyId++;
}
function SpawnWaves() {
for(var i = 0; i < enemycount; i++) {
var spawnPosition : Vector3 = new Vector3(Random.Range(-50, 50), spawnValues.y ,Random.Range (-50, 50));
var spawnRotation : Quaternion = Quaternion.identity;
SetName();
Instantiate(enemy, spawnPosition, spawnRotation);
enemiesarray.Push(this.enemy);
blipcontroller.CreateBlip(enemy, spawnPosition, spawnRotation);
}
}
BlipCtrl.js
public var EnemyBlipImage : Rigidbody;
public var bliparray = new Array();
public var blipID : int;
public var map : Minimap;
private var blipTarget : Blip;
private var gameobject : GameObject;
static var target : Transform;
var newBlip : GameObject;
function Awake () {
blipTarget = GetComponent(Blip);
map = GetComponent(Minimap);
}
function SetName() {
EnemyBlipImage.name = "EnemyBlip#" + blipID++;
}
function CreateBlip(object, objectName, position , rotation) {
SetName();
var newBlip = Instantiate(EnemyBlipImage, position, rotation);
newBlip.transform.SetParent(map.transform);
newBlip.gameObject.tag = "Enemy" ;
gameobject = GameObject.Find(objectName + "(Clone)");
blipTarget.target = gameobject.transform;
blipTarget.SetTarget(gameobject);
}
Blip.js
#pragma strict
public var target : Transform ;
public var keepInBounds = true;
public var LockScale = false;
public var LockRotation = false;
private var map : Minimap ;
private var myRectTransform : RectTransform ;
//set target to gameobject
function SetTarget(gameobject) {
target = gameobject;
}
function Start () {
map = GetComponentInParent(Minimap);
myRectTransform = GetComponent(RectTransform);
}
function Update () {
}
function LateUpdate() {
var newPosition : Vector3 = map.TransformPosition(target.position);
if(keepInBounds) {
newPosition = map.MoveInside(newPosition);
}
if(!LockScale) {
myRectTransform.localScale = new Vector3(map.zoomLevel, map.zoomLevel, 1);
}
if(!LockRotation) {
myRectTransform.localEulerAngles = map.TransformRotation(target.eulerAngles);
}
myRectTransform.localPosition = newPosition;
}
Comment