- Home /
Values not updating before object destruction.
I'm making a Space Invaders style video game for a game making competition. Part of the competition required the game to have some educational value, so I put a number above each enemy so the player would solve simple math problems by shooting the enemies. When a shot hits an enemy, it is supposed to take its individual value and set another variable equal to it, and then destroy itself. However, it seems that the enemies are not successfully assigning their values to the larger variables. My code for the two scripts concerned in this process goes as follows:
Enemy Destruction Script:
#pragma strict
public var scoreIncrease = 1;
public var mathNumbInput : float;
public var OnTopRow = false;
public var OnLeft = false;
public var OnRight = false;
var testForLowIncrease : float;
var testForUpIncrease : float;
function OnTriggerEnter2D (info : Collider2D)
{
if (info.name == "Shot")
{
if (GameMaster.mathNumber == 0)
{
GameMaster.mathNumber = mathNumbInput;
Debug.Log("Number Updated");
} else {
GameMaster.mathNumber2 = mathNumbInput;
Debug.Log("Number Updated");
}
if (OnTopRow == true)
{
if (testForLowIncrease == EnemyMovementScript.lowerBound)
{
if (OnLeft == true)
{
EnemyMovementScript.increaseLowerBound += 1;
EnemyMovementScript.increaseLowerBound = EnemyMovementScript.increaseLowerBound + EnemyMovementScript.lowIncreaseBonus;
EnemyMovementScript.lowIncreaseBonus = 0;
}
}
else
{
EnemyMovementScript.lowIncreaseBonus += 1;
}
if (testForUpIncrease == EnemyMovementScript.upperBound)
{
if (OnRight == true)
{
EnemyMovementScript.increaseUpperBound += 1;
EnemyMovementScript.increaseUpperBound = EnemyMovementScript.increaseUpperBound + EnemyMovementScript.upIncreaseBonus;
EnemyMovementScript.upIncreaseBonus = 0;
}
}
else
{
EnemyMovementScript.upIncreaseBonus += 1;
}
}
GameMaster.score += scoreIncrease;
GameMaster.levelAdvance += scoreIncrease;
Destroy(gameObject);
}
}
General game management script:
#pragma strict
var playerSpawner : Transform;
var playerSpawner2 : Transform;
static var mathNumber : float;
static var mathNumber2 : float;
public var test1 : float;
public var test2 : float;
var mathTotal : float;
static var mathGoal : float;
var level = 1;
var size1 : float = 200;
var size2 : float = 200;
var yOffset : float = 40;
static var score = 0;
static var levelAdvance = 0;
var lengthToWait = 100;
var waitTimer = 0;
var playerLife : Transform;
var gameOverScreen : Transform;
var playerLives = 4;
static var playerLifeDown = false;
function Start ()
{
mathGoal = Random.Range(2, 19);
}
function Update ()
{
if (mathGoal == 0)
{
mathGoal = Random.Range(2, 19);
}
test1 = mathNumber;
test2 = mathNumber2;
if (mathNumber > 0)
{
if (mathNumber2 > 0)
{
mathTotal = mathNumber + mathNumber2;
if (mathTotal == mathGoal)
{
Debug.Log("Well Done");
Instantiate(playerSpawner, Vector3(2.0f, 0, 0), transform.rotation);
Player_Movement_Script.playerSpritesOnScreen += 1;
mathNumber = 0;
mathNumber2 = 0;
mathTotal = 0;
mathGoal = Random.Range(2, 19);
}
else
{
mathNumber = 0;
mathNumber2 = 0;
mathTotal = 0;
mathGoal = Random.Range(2, 19);
}
}
}
}
if (playerLifeDown == true)
{
if (playerLives > 0)
{
EnemyMovementScript.speed = 0;
Player_Movement_Script.speed = 0;
Player_Movement_Script.playerCanShoot = false;
Enemy1Control.enemiesCanShoot = false;
if (waitTimer < lengthToWait)
{
waitTimer ++;
}
else
{
Instantiate(playerLife, transform.position, transform.rotation);
waitTimer = 0;
EnemyMovementScript.speed = EnemyMovementScript.saveSpeed;
Player_Movement_Script.speed = 10;
Player_Movement_Script.playerCanShoot = true;
Enemy1Control.enemiesCanShoot = true;
waitTimer = 0;
playerLifeDown = false;
playerLives -= 1;
}
}
else
{ playerLifeDown = false;
Instantiate(gameOverScreen, transform.position, transform.rotation);
}
}
}
function OnGUI ()
{
GUI.Box (new Rect (Screen.width/2-size1/2, yOffset, size1, size2), "Lives: " + playerLives + " Level: " + level + " Score: " + score + " Math Goal: " + mathGoal + " Current Number: " + mathNumber + " testing " + Player_Movement_Script.playerSpritesOnScreen);
}
Any advice or suggestions would be appreciated.
Answer by juicyz · Jan 06, 2017 at 06:05 PM
First off, I haven't done JS for a while but a couple of things I see that could lead to problems.
// You should default these to 0 or in the Start() set them to 0. They need an initial value
// Also there are several other values that aren't initialized and that's bad practice.
// Also why declare them as static var mathNumber : float and not static float mathNumber?
static var mathNumber : float;
static var mathNumber2 : float;
Ensure that mathNumbInput is actually a number. You should put some Debug.Log statements in your code to see what is actually happening and what the value of everything is. There a ton of things that could be happening but since I don't know the value of everything, I can't tell you.
Put some print statements in and tell me the values then we can work together and figure out your problem as there are too many hypotheticals right now and a lot of code to read... If you wrote this, you should be able to debug this pretty easily as it seems like a simple problem/
Also the value could be getting updated but maybe your GUI isn't updating. I haven't used OnGUI in a while, I think that's the old way and there is a newer GUI.
Yeah, I could have sworn that I had set mathNumbInput on every enemy, but apparently not. Sorry.
Your answer
Follow this Question
Related Questions
Unity value assignment help 0 Answers
Variable nos assigned on inspector, but it has been assigned on inspector ? 0 Answers
I want to assign multiple variables without having to type the class on the side every time. 1 Answer
How do I add something on to a float. 1 Answer
Making an enemy face the Player 0 Answers