I’m having a bit of trouble with my methods of linking, in that they are losing their connection after start…
I have three scripts that are all talking to each other:
LevelStatus, PlayerStatus, TargetController.
They all cache each others link at function Start().
They all pass the Debug.Log check so they are (theoretically) linked and cached at Start().
When I call from TargetController.js a function in PlayerStatus.js that checks a variable in LevelStatus.js, I get a null exception “no link to LevelStatus”.
I can force a link by refinding LevelStatus within the function, but that’s inefficient. I feel that I’ve made the link and cached it in function Start() and shouldn’t have to do it again.
I’ve bugged the sheets off the folks in the IRC channel, and there’s been no light in the wood shed so far.
Ultimately what I’m doing is:
Here are the relevant bits of code:
LevelStatus.js contains some level based variables. This script is overkill right now, but design-wise should be where this code is for futureproofing.
LevelStatus.js
var setLives : int;
var setMaxFuel : int;
var setHealth : int;
//var playerStatus : PlayerStatus; // don't need the link in this script right now
function Start () {
Debug.Log("LevelStatus.js Starting");
}
function OnDisable ()
{
Debug.Log("LevelStatus.js STOPPED");
}
function Reset () {
setLives = 5;
setMaxFuel = 1000;
setHealth =100;
}
(The Debug.Log is from troubleshooting…)
PlayerStatus.js is where I keep most of the immediate variables and functions related to the player state.
PlayerStatus.js
static var LTOFuel : float;
static var LTOHealth : float;
private var shipController : ShipController;
private var levelStatus : LevelStatus;
//var shipController : ShipController;
//var levelStatus : LevelStatus;
function Start () {
shipController = FindObjectOfType(ShipController);
if (!shipController)
Debug.Log("Player Status Start: No link to Spaceship Controller");
levelStatus = FindObjectOfType(LevelStatus);
if (levelStatus == null)
Debug.Log("Player Status Start: No link to Level Status");
LTOFuel = levelStatus.setMaxFuel;
Debug.Log("LTOFuel: " + LTOFuel);
Debug.Log("levelStatus.setMaxFuel: " + levelStatus.setMaxFuel);
LTOHealth = levelStatus.setHealth;
LTOLives = levelStatus.setLives;
}
function ReFuel (reFuelValue : int) {
LTOFuel += reFuelValue;
levelStatus = FindObjectOfType(LevelStatus);
if (levelStatus == null)
Debug.Log("Player Status Refuel: No link to Level Status");
if (levelStatus) {
if (LTOFuel > levelStatus.setMaxFuel) {
LTOFuel = levelStatus.setMaxFuel;
}
}
}
function Repair (repairlValue : int) {
LTOHealth += repairlValue;
levelStatus = FindObjectOfType(LevelStatus);
if (levelStatus == null)
Debug.Log("Player Status Repair: No link to Level Status");
if (levelStatus) {
if (LTOHealth > levelStatus.setHealth) {
LTOHealth = levelStatus.setHealth;
}
}
}
TargetController makes function calls to PlayerStatus depending upon several tests.
TargetController.js
private var playerStatus : PlayerStatus;
private var levelStatus : LevelStatus;
private var announcement : GUIAnnouncement;
private var refuelRation : int;
private var repairRation : int;
function Start () {
playerStatus = FindObjectOfType(PlayerStatus);
if (!playerStatus)
Debug.Log("LZC: No link to Player Status");
levelStatus = FindObjectOfType(LevelStatus);
if (!levelStatus)
Debug.Log("LZC: No link to Level Status");
announcement = FindObjectOfType(GUIAnnouncement);
if (!announcement)
Debug.Log("LZC: No link to GUI Announcement");
}
//SNIP! removed lots of code, including the tests to check target success and if tests are true, call the function "TargetSuccess" below:
function TargetSuccess () {
// announce the status
announcement.textMessage = "Success";
yield WaitForSeconds (1);
announcement.textMessage = "Success\n\nYou did it!";
// do the work
RefuelShip ();
RepairShip ();
// reset the announcement
yield WaitForSeconds (3);
announcement.textMessage = "";
}
function RefuelShip () {
playerStatus.ReFuel(refuelRation);
}
function RepairShip () {
playerStatus.Repair(repairRation);
}