Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Int cannot convert to string

Discussion in 'Scripting' started by platch13, Jun 28, 2020.

  1. platch13

    platch13

    Joined:
    Dec 31, 2019
    Posts:
    7
    I want to show the speed of my player in a text. The player gets speed with pressing space. I tried GUILayout.Label(Speedshow.ToString()); and
    Speedshow.text = Speedshownumber.ToString(); but it dosnt work.









    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.     public CharacterController controller;
    9.  
    10.     public int speed;
    11.     public float jumpHeight = 3f;
    12.  
    13.     Vector3 velocity;
    14.     public float gravity = -9.81f;
    15.     public Transform groundCheck;
    16.     public float groundDistance = 0.4f;
    17.     public LayerMask groundMask;
    18.     bool isGrounded;
    19.     public int speed0 = 0;
    20.     public int space1 = -13;
    21.     int canceltest = 99;
    22.     int spacecounter = 2;
    23.     int escsave;
    24.     public int escpushed;
    25.     public Text Speedshow;
    26.     public bool Speedshownumber;
    27.  
    28.  
    29.     void Start()
    30.     {
    31.         speed0 = 0;
    32.         print(speed0);
    33.         speed = speed0;
    34.         spacecounter = 2;
    35.         escsave = 2;
    36.        
    37.     }
    38.  
    39.     void Update()
    40.     {
    41.  
    42.         if (Input.GetKeyDown(KeyCode.Escape))
    43.         {
    44.             escsave = spacecounter;
    45.             escpushed = escpushed + 1;
    46.         }
    47.  
    48.         if (escpushed % 2 == 0)
    49.         {
    50.            // print(escpushed);
    51.  
    52.             if (spacecounter >= 1 && spacecounter < 999)
    53.             {
    54.  
    55.  
    56.  
    57.                 if (Input.GetKey(KeyCode.Space))
    58.                 {
    59.  
    60.                     print(speed);
    61.                     speed = speed + 1;
    62.                     print(speed);
    63.                     //Speedshow.text = Speedshownumber.ToString();
    64.                     //GUILayout.Label(Speedshow.ToString());
    65.                     Speedshow.GetComponent<Text>().text = speed;
    66.  
    67.                 }
    68.  
    69.                 if (Input.GetKeyUp(KeyCode.Space))
    70.                 {
    71.                     StartCoroutine(Release());
    72.                     spacecounter = spacecounter - 1;
    73.                     print(spacecounter);
    74.                     escsave = spacecounter;
    75.                 }
    76.                 if (Input.GetKeyDown(KeyCode.Space))
    77.                 {
    78.                     StopAllCoroutines();
    79.  
    80.                 }
    81.             }
    82.         }
    83.  
    84.         else { }
    85.  
    86.  
    87.             isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    88.  
    89.         if(isGrounded && velocity.y < 0)
    90.         {
    91.             velocity.y = -2f;
    92.         }
    93.  
    94.         float x = Input.GetAxis("Horizontal");
    95.         float z = Input.GetAxis("Vertical");
    96.  
    97.         Vector3 move = transform.right * x + transform.forward * z;
    98.  
    99.         controller.Move(move * speed * Time.deltaTime);
    100.  
    101.         if(Input.GetButtonDown("Jump") && isGrounded)
    102.         {
    103.             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    104.         }
    105.  
    106.         velocity.y += gravity * Time.deltaTime;
    107.        
    108.         controller.Move(velocity * Time.deltaTime);
    109.     }
    110.  
    111.     private IEnumerator Release()
    112.     {
    113.  
    114.         print(space1);
    115.         yield return new WaitForSeconds(2.0f);
    116.         speed = 0;
    117.         space1 = space1 + 1;
    118.         print(space1);
    119.         yield return new WaitForSeconds(2.0f);
    120.         print(canceltest);
    121.  
    122.  
    123.     }
    124.  
    125. }
    126.  
    My VisualStudio is set to german.

    Thanks in advance.
     

    Attached Files:

  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    What does "doesn't work" mean? Did you get an error? (Remember to check your console.) If so, what was the error message, and on what exact line did it occur? If not, what actually happens in the game?

    Note that
    Speedshownumber
    is actually not a number at all; on line 26 you have it declared as a
    bool
    . That means ToString should give you either "True" or "False".
     
  3. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    Code (CSharp):
    1.  Speedshow.text = speed.ToString();