Hello,
Having a bit of a weird problem with losing the reference between Script A to object B (TempSaveObject) and its script (TempSaveScript).
When Object A is instantiated, (its script) Script A gets a reference to TempSaveObject and its TempSaveScript. This is done in the Start() Method.
// Get the tempSave Object and script
tempSaveObject = (GameObject) GameObject.FindWithTag("TempSaveObject");
// Check if tempSaveObject has been found
if(tempSaveObject == null)
{
Debug.Log("EnemyScript.Start(): TempSaveObject is Null");
}
else
{
Debug.Log("EnemyScript.Start(): TempSaveObject is not Null");
}
tempSaveScript = (TempSaveScript) tempSaveObject.GetComponent("TempSaveScript");
// Check if tempSaveScript has been found
if(tempSaveScript == null)
{
Debug.Log("EnemyScript.Start(): TempSaveScript is Null");
}
else
{
Debug.Log("EnemyScript.Start(): TempSaveScript is not Null");
}
The debug prints that TempSaveObject and TempSaveScript are NOT null. Shortly after being created, a method is called on Script A. This method references a public field on TempSaveScript. At this point, however, both TempSaveObject and TempSaveScript are returning as null. I can see that TempSaveObject still exists in the scene.
I can rectify the problem by creating a new reference to tempSaveObject and TempSaveScript, (as below), but I'd like to get to the root of the problem. Does anyone have any idea what could cause the reference to be lost? Thanks for your help.
Re-establishing the reference:
// Testing: Check if tempSaveObject is null
if(tempSaveObject == null)
{
Debug.Log("EnemyScript.AllocateStatPoints: tempSaveObject Is Null 1");
tempSaveObject = GameObject.FindGameObjectWithTag("TempSaveObject");
if(tempSaveObject == null)
{
Debug.Log("EnemyScript.AllocateStatPoints: tempSaveObject Is Null 2");
}
}
// Testing: Check if tempSaveScript is null
if(tempSaveScript == null)
{
Debug.Log("EnemyScript.AllocateStatPoints: tempSaveScript Is Null 1");
tempSaveScript = (TempSaveScript) tempSaveObject.GetComponent("TempSaveScript");
if(tempSaveScript == null)
{
Debug.Log("EnemyScript.AllocateStatPoints: tempSaveScript Is Null 2");
}
}
TempSave is null 1 prints, but then the reference is re-established. Any help with this would be greatly appreciated.
↧