I have run into a problem that is now bugging the hell out of me. I am making a 2D Space Shooter at the moment and I made a randomdrop script and a enemy ship script which I made drop the random drop chosen upon death. I only kept this script on one ship while testing and working out the kinks and it worked fine. This is why it confuses me. I’m using the exact same script on the exact same ship, unchanged, but when I added it to the other ships and made other few minor changes it stopped the game upon dropping the boost and the enemyship getting destroyed and gave me this error:
NullReferenceException: Object reference not set to an instance of an object
iTween.RetrieveArgs () (at Assets/Plugins/Pixelplacement/iTween/iTween.cs:6812)
iTween.Awake () (at Assets/Plugins/Pixelplacement/iTween/iTween.cs:6559)
UnityEngine.Object:Instantiate(Object, Vector3, Quaternion)
Bantera1script:Update() (at Assets/Level1/Scripts/Bantera1script.js:49)
I can then proceed to unpause the game and it will work like as if nothing happened and then when another ship gets killed and happens to drop a boost it happens again. This is extremely irritating and I have rolled back all the changes from the point when I added it to the other ships and still no fix. Here are my enemy ship and randomdrop scripts:
Random drop script:
#pragma strict
var rdsSelect:int;
static var dropType:boolean[];
static var scoreMult:boolean;
function Start () {
dropType = [false,false];
scoreMult = false;
SelectRandomNumber();
if(rdsSelect <= 6){
//Player gets nothing
dropType[0] = true;
}
if(rdsSelect <= 10 && rdsSelect >= 7){
dropType[1] = true;
}
}
function Update () {
}
function SelectRandomNumber () {
rdsSelect = Random.Range(1,10);
}
Enemy Ship:
#pragma strict
var speed:float;
var player:playerscript; //Variable to link to the players script
var bullet:Transform;
var canShoot:boolean = true;
var gun1:Transform;
var gun2:Transform;
var hp:int;
var shot:AudioSource;
var damage:AudioSource;
static var rdsDrop:int;
function Start () {
var multSources : AudioSource[] = gameObject.GetComponents.<AudioSource>();
shot = multSources[0];
damage = multSources[1];
player = GameObject.FindGameObjectWithTag("Player").GetComponent(playerscript);
hp = 3;
iTween.MoveTo(gameObject,{"y":12,"time":4,"delay":1});
iTween.MoveTo(gameObject,{"x":-21,"y":12,"time":3,"delay":5});
iTween.MoveTo(gameObject,{"x":21,"time":10,"delay":10});
if(randomdrop.dropType[0] == true){
Debug.Log("No Drop");
}
if(randomdrop.dropType[1] == true){
SelectRandomDrop();
Debug.Log("dp" + rdsDrop);
}
}
function Update () {
var dp1 = GameObject.Find("2xboost");
var dp2 = GameObject.Find("healthboost");
var dp3 = GameObject.Find("superboost");
var dp4 = GameObject.Find("clearboost");
var enemyObject = GameObject.Find("bantera1");
var enemyPos:Vector3 = enemyObject.transform.position;
if(canShoot == true) {
Instantiate(bullet,gun1.position,Quaternion.identity);
Instantiate(bullet,gun2.position,Quaternion.identity);
canShoot = false;
shot.Play();
Reload();
}
if(hp <= 0){
Death();
if(rdsDrop == 1){
Instantiate(dp1,enemyPos,Quaternion.identity);
}
if(rdsDrop == 2){
Instantiate(dp2,enemyPos,Quaternion.identity);
}
if(rdsDrop == 3){
Instantiate(dp3,enemyPos,Quaternion.identity);
}
}
}
function Reload () {
yield WaitForSeconds(1.5);
canShoot = true;
}
function OnTriggerEnter2D(other:Collider2D){
//Check to see if what we hit was the player
if(other.tag == "Player") {
player.hp --;
hp --;
//Play damage sound
damage.Play();
//If player bullet hits destroy bullet and ship
}
if(other.tag == "Bullet") {
if (randomdrop.scoreMult == false){
//Add points to player score
player.score += 1;
playerscript.denarii += 2;
}else{
player.score /= .9;
}
damage.Play();
//Destroy the bullet
Destroy(other.gameObject);
hp --;
}
if(other.tag == "Bullet2") {
if (randomdrop.scoreMult == false){
player.score += 2;
playerscript.denarii += 3;
}else{
player.score /= .8;
}
damage.Play();
Destroy(other.gameObject);
hp -= 2;
}
if(other.tag == "Bullet3") {
if (randomdrop.scoreMult == false){
player.score += 3;
playerscript.denarii += 4;
}else{
player.score /= .7;
}
damage.Play();
Destroy(other.gameObject);
hp -= 3;
}
}
function SelectRandomDrop () {
rdsDrop = Random.Range(1,3);
}
function Death () {
damage.Play();
Destroy(gameObject);
}
I’m thinking about re-doing the way I made it and instead of calling the boost from the GameObjects, making a transform variable and just putting the boost prefabs in there for spawning. Hopefully there’s a way to fix this without doing that though.