Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Bexxth · Oct 28, 2013 at 09:54 AM · if statement

If statment not printing to console.

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
         
         }
 
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Magnomous · Oct 28, 2013 at 09:58 AM 0
Share

Try to use Debug.Log("...") ins$$anonymous$$d of print("....")

avatar image Bexxth · Oct 28, 2013 at 10:07 AM 0
Share

Nope, still does nothing.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Crystalline · Oct 28, 2013 at 10:20 AM

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
  
        }
  
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by EvilWarren · Oct 28, 2013 at 10:23 AM

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){

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bexxth · Oct 28, 2013 at 10:29 AM 0
Share

Brilliant! Thanks for the help guys, that works perfectly now!

playerLives was set so low because I got tired of killing the player while testing to see if the print()was working XD

thanks again!

avatar image EvilWarren · Oct 28, 2013 at 10:36 AM 0
Share

If your question is answered, accept one of the answers. Probably accept Crystalline's, as it was faster than $$anonymous$$e by 3 $$anonymous$$utes :(

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

two if() statements with one GetKeyDown won't work. 1 Answer

if statement problem ! 1 Answer

Both sides of my IF statement seem to be firing 2 Answers

what would be the if statement if the previous question loaded is repeated or equal to the same.. 1 Answer

Bool based on objects existing not changing. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges