This is probably another silly mistake but I am super new to this and I can’t figure it out.
No error, however the if statements should print to the console, however they are’t. Even when an application is attached rather than asking to print to console the if statements don’t work. I am following a tutorial and as far as I can see my code is identical but it just doesn’t work.
Lines 30 - 38 is where I think the trouble is.
var playerSpeed : int; //movement speed
var playerLives : int; //player lives
static var playerScore : int; //player score
var bullet: Rigidbody; //allows player and bullet to be connected
var explosion: Transform; //enables explosion
function Update (){
amtToMove = (playerSpeed * Input.GetAxis("Horizontal")) * Time.deltaTime; //amount to move
transform.Translate(Vector3.right * amtToMove); //move player
if(Input.GetKeyDown("space")){ //press space for bullet
var tempBullet: Rigidbody;
tempBullet = Instantiate(bullet, transform.position, transform.rotation); //bullet appears from player's location
}
}
if(playerScore >= 100){
print("YOU WON THE GAME");
}
if(playerLives <=1){
print("game over");
}
//sets up the GUI for score and lives
function OnGUI(){
GUI.Label(Rect(10,10,200,50),"Score: " + playerScore); //puts the player's score in the GUI
GUI.Label(Rect(10,30,200,50),"Lives: " + playerLives); //puts the player's lives in the GUI
}
function OnTriggerEnter(otherObject: Collider){
if(otherObject.gameObject.tag == "enemy"){ //if enemy hits player
otherObject.gameObject.transform.position.y =7; //puts back up to top of the screen after collision
otherObject.gameObject.transform.position.x = Random.Range(-6,6); // resets enemy to random position along x-axis
var tempExplosion: Transform; //sets the explosion
tempExplosion = Instantiate(explosion, transform.position,transform.rotation); //places explosion on the same spot as bullet
playerLives --; //takes one life
}
}
It seems like this part of code is out of Update loop.
if(playerScore >= 100){
print("YOU WON THE GAME");
}
if(playerLives <=1){
print("game over");
}
Instead, try this:
var playerSpeed : int; //movement speed
var playerLives : int; //player lives
static var playerScore : int; //player score
var bullet: Rigidbody; //allows player and bullet to be connected
var explosion: Transform; //enables explosion
function Update (){
amtToMove = (playerSpeed * Input.GetAxis("Horizontal")) * Time.deltaTime; //amount to move
transform.Translate(Vector3.right * amtToMove); //move player
if(Input.GetKeyDown("space")){ //press space for bullet
var tempBullet: Rigidbody;
tempBullet = Instantiate(bullet, transform.position, transform.rotation); //bullet appears from player's location
}
if(playerScore >= 100){
print("YOU WON THE GAME");
}
if(playerLives <=1){
print("game over");
}
}
//sets up the GUI for score and lives
function OnGUI(){
GUI.Label(Rect(10,10,200,50),"Score: " + playerScore); //puts the player's score in the GUI
GUI.Label(Rect(10,30,200,50),"Lives: " + playerLives); //puts the player's lives in the GUI
}
function OnTriggerEnter(otherObject: Collider){
if(otherObject.gameObject.tag == "enemy"){ //if enemy hits player
otherObject.gameObject.transform.position.y =7; //puts back up to top of the screen after collision
otherObject.gameObject.transform.position.x = Random.Range(-6,6); // resets enemy to random position along x-axis
var tempExplosion: Transform; //sets the explosion
tempExplosion = Instantiate(explosion, transform.position,transform.rotation); //places explosion on the same spot as bullet
playerLives --; //takes one life
}
}
Your if statements are outside the Update() function. You need to put them between the curly braces {} associated with Update().
function Update (){
amtToMove = (playerSpeed * Input.GetAxis("Horizontal")) * Time.deltaTime; //amount to move
transform.Translate(Vector3.right * amtToMove); //move player
if(Input.GetKeyDown("space")){ //press space for bullet
var tempBullet: Rigidbody;
tempBullet = Instantiate(bullet, transform.position, transform.rotation);//bullet appears from player's location
}
if(playerScore >= 100){
print("YOU WON THE GAME");
}
if(playerLives <=1){
print("game over");
}
}
Also the playerLives you have less than or equal to 1, which means if the player has 1 life left, he loses. Is this intentional? Might make more sense to be less than 1 i.e. if(playerLives <1){