- Home /
Script is setting variables but they're being reset
In the game I'm working on the player has a flare gun with a screen that displays how many flares they have available. There's an item pickup that increases the maximum flare amount, here's the code:
//increase values in flare script</p> <p>GameObject.Find("Near Camera").GetComponent("FlareGun").flareGlobalMax=4; GameObject.Find("Near Camera").GetComponent("FlareGun").redMax=4;</p> <p>GameObject.Find("Near Camera").GetComponent("FlareGun").greenMax=2;</p> <p>GameObject.Find("Near Camera").GetComponent("FlareGun").blueMax=3;</p> <p>//increase in screen script</p> <p>GameObject.Find("Screen").GetComponent("FlareScreen").globalMax=4; GameObject.Find("Screen").GetComponent("FlareScreen").maxRed=4; GameObject.Find("Screen").GetComponent("FlareScreen").maxBlue=3; GameObject.Find("Screen").GetComponent("FlareScreen").maxGreen=2;</p> <p>Debug.Log("Reset");</p> <p>GameObject.Find("Screen").GetComponent("FlareScreen").PickMat();
The values on FlareGun are being set correctly, however the FlareScreen values are being set but apparently revert immediately. The "Reset" debug is printing and I can print out the value of globalMax during the function PickMat however on subsequent executions of PickMat globalMax is reset to 3.
See anything? Any help would be greatly appreciated.
It's hard to see just from the code above what the error could be. If Debug.Log("Reset") appears in the log as you say, however, then we can safely assume that this script does set global$$anonymous$$ax = 4. Therefore, if it is 3 again later on in Pick$$anonymous$$at, it has to be because it is being set somewhere else in your program. Do a search through all of your source code for all references of global$$anonymous$$ax and trace through where it is being set. Does it appear in some Update() call of a different GameObject, for example?
Is this code executing during initialization? Inside Start() or Awake().
Answer by M Poirot · May 02, 2011 at 02:45 PM
You might wanna post your script as a code example like
function Awake()
{
Instantiate (CAMstartPrefab, Vector3(0, 0, 0), Quaternion.identity);
}
because it't damn though to read it this way