Hi all!
So I’m trying to inicialize a variable of type “SaveShip” with the following constructors and instance variables (note please, SaveShip extends from Object):
var shipPrefab : GameObject;
var shipInfo : ShipInfo;
var shipHea : ShipHealth;
var shipInv : ShipInventory;
var dilithium : int;
function SaveShip() {
this.shipInfo = new ShipInfo();
this.shipHea = new ShipHealth();
this.shipInv = new ShipInventory();
shipPrefab = null;
dilithium = 0;
}
function SaveShip(ship : GameObject) {
SaveShip();
setShip(ship);
}
function SaveShip(ship : GameObject, faction : int) {
SaveShip(ship);
shipInfo.Faction = faction;
}
However, when I call the constructor with two parameters, the variables inicialized when it gets to call the “SaveShip()” constructor don’t get inicialized at all! In MonoDevelop Debuger, I can see the variables are still at “(null)”. So it’s there any issue here with these constructors? Or is this a GC issue or something?
EDIT: here’s the SaveShip.setShip(ship : GameObject) function:
//this function stores the ship here
function setShip(ship : GameObject) {
hasBeenSet = true;
//now lets fill the information
//first get the scripts
var shipProps : shipProperties = ship.GetComponent(shipProperties);
var shipHeal : shipHealth = ship.GetComponent(shipHealth);
var shipWea : shipWeapons = ship.GetComponent(shipWeapons);
var shipFuel : ShipFuel = ship.GetComponent(ShipFuel);
var up : Upgrades = ship.GetComponent(Upgrades);
//now fill the ship info part
shipInfo.Name = shipProps.shipInfo.shipName;
shipInfo.Faction = shipProps.shipInfo.faction;
shipInfo.isPlayer = shipProps.playerProps.isPlayer;
shipInfo.isRedAlert = shipProps.combatStatus.isRedAlert;
shipInfo.strenght = shipProps.shipProps.shipStrenght;
//now fill the ship health part
shipHea.curHull = shipHeal.shipHealth.health;
shipHea.curShield = shipHeal.shipHealth.shields;
//and now the inventory part
//first get the weapon game objects of each weaponslot
shipInv.phaser = shipWea.phaser.phaser;
shipInv.torp1 = shipWea.torp1.torpedo;
shipInv.torp2 = shipWea.torp2.torpedo;
//get upgrades
shipInv.upgrades = up.upgrades;
//get dilithium
dilithium = shipFuel.getCurrentLoad();
//now get load a prefab for this ship
shipPrefab = Resources.Load(ship.name) as GameObject;
}
EDIT II: And here’s where I’m calling the constructor, in a completely unrelated script:
private function setConquerFleet(fleet : List.<GameObject>, faction : int) {
for(var x : int = 0; x < 6; x++) {
if(fleet.Count > 0) {
var ship : GameObject = fleet[Random.Range(0, fleet.Count - 1)];
var newShip : SaveShip = new SaveShip(ship, faction);
defenseFleet.Add(newShip);
}
}
}
Where defenseFleet is a list of “SaveShip”