Search Unity

Trying to print using C#

Discussion in 'Scripting' started by milar_ninja, Jun 23, 2010.

Thread Status:
Not open for further replies.
  1. milar_ninja

    milar_ninja

    Joined:
    Jun 11, 2010
    Posts:
    21
    Ok so I have this little game I am working on and I am tring to get some output for the user when he hits the enemy I would like to print out something so the player can see hey I hit the opponet. Just some simple line somewhere so I can test to see if my code is acctually doing something.
    I have tried print("something");
    and also Console.Writeline("something");
    too see if I did the scripting right;
    Thanks:)
    here is the code:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4. using System.Collections.Generic;
    5. using System.Text;
    6. using System.Threading;
    7.  
    8.  
    9. public class Battle : MonoBehaviour {
    10.    
    11.         //build and initialization of characters in the game
    12.         //do this somewhere else in real game
    13.         //build character 1
    14.         Player Mav = new Player();
    15.        
    16.         //build enemy ninja
    17.         Enemy Ninja = new Enemy();
    18.        
    19.     public double potion = 100;//hp boost
    20.     public double elixer = 200;//hp boost
    21.    
    22.    
    23.  
    24.     // Use this for initialization
    25.     void Start () {
    26.        
    27.  
    28.         Mav.hp = 1220;
    29.         Mav.mp = 150;
    30.         Mav.sp = 100;
    31.         Mav.charaterName = "Mavrick";
    32.        
    33.         //initialization of attacks and skill wheel
    34.         Mav.slash = 1;
    35.         Mav.flameSlash = 1.5;
    36.         Mav.shiningStrike = 2;
    37.        
    38.         //skill wheel
    39.         Mav.powerDive = 150;
    40.         Mav.powerCharge = 250;
    41.         Mav.wildStrike = 350;
    42.         Mav.renegade = 400;
    43.         Mav.shiningStars = 500;
    44.        
    45.         //magic
    46.         Mav.lightSpell = 150;
    47.         Mav.flame = 150;
    48.         Mav.snow = 300;
    49.        
    50.        
    51.         //initialization of enemy
    52.         Ninja.hp = 1000;
    53.         Ninja.atk1 = 100;
    54.         Ninja.atk2 = 150;
    55.         Ninja.specialAttack = 200;
    56.         Ninja.enemyName = "Ninja";
    57.        
    58.     }
    59.    
    60.     // Update is called once per frame
    61.     void Update () {
    62.         battle();
    63.                
    64.     }
    65.    
    66.    
    67.     void battle()
    68.     {
    69.         //see if enemy dies
    70.         while(Ninja.hp > 0){
    71.            
    72.         //random number for attack power Not C# but unity engine random number
    73.         int dice = UnityEngine.Random.Range(1,50);
    74.         string bInput;
    75.         char button;
    76.         bInput = Console.ReadLine();
    77.         button = char.Parse(bInput);
    78.        
    79.         switch (button)
    80.                 {
    81.                     //slash attack
    82.                     case 'x':
    83.                         Ninja.hp = Ninja.hp - (Mav.slash * dice);
    84.                         Console.WriteLine("Slash Attack");
    85.                         print("Slash Attack");
    86.                         print(Ninja.hp);
    87.  
    88.                         Thread.Sleep(5000);//sleep for 5 second Player cant attack
    89.                        
    90.                         break;
    91.  
    92.                     //flame slash attack
    93.                     case 'v':
    94.  
    95.                         Ninja.hp = Ninja.hp - (Mav.flameSlash * dice);
    96.                        
    97.                         Thread.Sleep(5000);//sleep for 5 second Player cant attack
    98.                        
    99.                         break;
    100.  
    101.                     //shining Srike attack
    102.                     case 's':
    103.  
    104.                         Ninja.hp = Ninja.hp - (Mav.shiningStrike * dice);
    105.                        
    106.                         Thread.Sleep(5000);//sleep for 5 second Player cant attack
    107.                        
    108.                         break;
    109.  
    110.                     //skill wheel attacks
    111.                     case 'l':
    112.                     /*    
    113.                     Console.WriteLine("Skill Wheel");
    114.                         Console.WriteLine("1 for Power Dive");
    115.                         Console.WriteLine("2 for Power Charge");
    116.                         Console.WriteLine("3 for Wild Strike");
    117.                         Console.WriteLine("4 for Renegade");
    118.                         Console.WriteLine("5 for Shining Stars");
    119.                     */
    120.                    
    121.                         string b1Input;
    122.                         int button1;
    123.                         b1Input = Console.ReadLine();
    124.                         button1 = int.Parse(b1Input);
    125.                    
    126.                         //inner if else
    127.                         if (button1 == 1)
    128.                         {
    129.                             Ninja.hp -= Mav.powerDive;
    130.  
    131.                         }
    132.                         else if (button1 == 2)
    133.                         {
    134.                             Ninja.hp -= Mav.powerCharge;
    135.  
    136.                         }
    137.                         else if (button1 == 3)
    138.                         {
    139.                             Ninja.hp -= Mav.wildStrike;
    140.  
    141.                         }
    142.                         else if (button1 == 4)
    143.                         {
    144.                            Ninja.hp -= Mav.renegade;
    145.                         }
    146.                         else if (button1 == 5)
    147.                         {
    148.                             Ninja.hp -= Mav.shiningStars;
    149.                         }
    150.                        
    151.                         break;
    152.                        
    153.                     //magic attacks
    154.                     case 'r':
    155.                     /*
    156.                         Console.WriteLine("Press 1 for Light");
    157.                         Console.WriteLine("Press 2 for Flame");
    158.                         Console.WriteLine("Press 3 for Snow");
    159.                     */
    160.  
    161.                         string b2Input;
    162.                         int button2;
    163.                         b2Input = Console.ReadLine();
    164.                         button2 = int.Parse(b2Input);
    165.                      
    166.                         //inner if else
    167.                         if (button2 == 1)
    168.                         {
    169.                             Mav.hp += Mav.lightSpell;
    170.                            
    171.                         }
    172.                         else if (button2 == 2)
    173.                         {
    174.                             Ninja.hp -= Mav.flame;
    175.                         }
    176.                         else if (button2 == 3)
    177.                         {
    178.                             Ninja.hp -= Mav.snow;
    179.                         }
    180.                        
    181.                         break;
    182.  
    183.                     case 'i':
    184.                        //use for items
    185.  
    186.                         string b3Input;
    187.                         int button3;
    188.                         b3Input = Console.ReadLine();
    189.                         button3 = int.Parse(b3Input);
    190.                        
    191.                     if(button3 ==1)
    192.                         {
    193.                             Mav.hp += potion;
    194.                         }else if(button3 == 2)
    195.                         {
    196.                             Mav.hp += elixer;
    197.                         }
    198.                                                                    
    199.                         break;
    200.  
    201.                     case 'd':
    202.                         //use for defense
    203.                         break;
    204.  
    205.                     case 'c':
    206.                         //charater switch command
    207.  
    208.                         break;
    209.  
    210.  
    211.                 }//end of switch for input
    212.             }//end of while loop
    213.  
    214.     }
    215. }
    216.  
     
    tigerleapgorge likes this.
  2. RamenNoodles

    RamenNoodles

    Joined:
    Apr 8, 2010
    Posts:
    19
    I use Debug.Log this will display in a console that can be accessed by double clicking the bottom status bar on unity or SHIFT + CTRL + C.

    Put

    Debug.Log("hit");

    in whichever if statment you have the hit in.

    Hope that helps. If you want something that would display to the player then I'm not too sure about that, I'm thinking to using the gameobject GUI TEXT.
     
  3. milar_ninja

    milar_ninja

    Joined:
    Jun 11, 2010
    Posts:
    21
    Ok so I put in the Debug.Log("hit"); in to the game when ever I have an attack and I think that the game is not handleing the input right becuase nothing is happening. Do I have to use the Unity input instead of C#s way of handiling input?
     
  4. RamenNoodles

    RamenNoodles

    Joined:
    Apr 8, 2010
    Posts:
    19
    I believe so, in the tutorial I used to learn unity they used GUI Text to display text to the user, so thats what I do to display any text. If there is a better way I wouldnt know it, sorry.

    EDIT: Oops I thought you we're still talking about display text to the user. For button input, most likely you can use,

    Input.GetKey(KeyCode.W)

    to get a specific keyboard key.

    The tutorials use

    EDIT > PROJECT SETTINGS > INPUT

    to handle keys and inputs.
     
  5. mstarr

    mstarr

    Joined:
    Jun 20, 2018
    Posts:
    135
    So does anyone know why this is the first hit for "unity C# printing to player output" if Debug.Log prints to the game designer?
     
  6. siddharthdprajapati

    siddharthdprajapati

    Joined:
    Aug 20, 2023
    Posts:
    2
    I am working on a game. When I was learning How to make game I saw that a command is used that is "print("");"
    but in my project when I go to use this command it was not working
     
  7. siddharthdprajapati

    siddharthdprajapati

    Joined:
    Aug 20, 2023
    Posts:
    2
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Please don't necro-post / hijack threads on these forums. Create your own threads for your own problems.

    Thanks.
     
Thread Status:
Not open for further replies.