0

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 NullReferenceException 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;

}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 2
    Do not use "unityscript" with Unity. You'll have to change to c#. They're getting rid of "unityscript" soon (see Unity blog). Note that c# is actually considerably easier. – Fattie Jan 28 '16 at 13:47
  • Learn to use `Debug.Log`. Include about ten `Debug.Log` lines in your code and you will know the problem in seconds. – Fattie Jan 28 '16 at 13:48
  • I used Debug.Log and found that that the GameObject.Find(gameObject) was returning null. Then added GameObject.Find(gameObject + "(Clone)"); and then found the object. But still can't set the Target in the Blip script to the gameObject.transform – Merlin Jackson Jan 28 '16 at 17:09
  • 1
    Try renaming the private GameObject gameObject to something else. gameObject.transform might return the transform of the gameObject BlipCtrl is attached to, and not the gameObject's transform you've searched for – Ilona Hari Jan 29 '16 at 07:29

0 Answers0