Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

why do i get the "cant convert float to int" error

Discussion in 'Scripting' started by kancelarija12, Oct 28, 2019.

  1. kancelarija12

    kancelarija12

    Joined:
    Sep 17, 2019
    Posts:
    15
    i really dont know why this happens and it happens too much times for me
    so here is the part of the script that uses floats but if the whole script is needed im gonna paste it.
    i pasted the real one now since you got confused

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class lobbyanddead : MonoBehaviour
    7. {
    8.     public GameObject play;
    9.     public GameObject scp457;
    10.     public GameObject scp008;
    11.     public GameObject scp173;
    12.     public float blink = 4.0f;
    13.     public int health = 100;
    14.     public bool infected = false;
    15.  
    16.     void Update()
    17.     {
    18.         blink -= 1.0f * Time.deltaTime;
    19.         if(blink <= 0.9f)
    20.         {
    21.             blink = 4.0f;
    22.         }
    23.         if (health <= 1);
    24.         {
    25.             health = 100;
    26.             transform.position = new Vector3(10, 1, -400);
    27.             transform.Rotate(0, 0, 0);
    28.         }
    29.         if(infected == true)
    30.         {
    31.             health -= 1 * Time.deltaTime;
    32.         }
    33.     }
    34.  
    35.     void OnCollisionEnter(Collision collision)
    36.     {
    37.         if (collision.gameObject == play)
    38.         {
    39.             transform.position = new Vector3(22, 1, -69);
    40.             transform.Rotate(0, 0, 0);
    41.         }
    42.         else if (collision.gameObject == scp457)
    43.         {
    44.             transform.position = new Vector3(10, 1, -400);
    45.             transform.Rotate(0, 0, 0);
    46.         }
    47.         else if (collision.gameObject == scp008)
    48.         {
    49.             health -= 10;
    50.             infected = true;
    51.             transform.Translate(Vector3.forward * Time.deltaTime * 10);
    52.         }
    53.         else if (collision.gameObject == scp173)
    54.         {
    55.             if(blink <= 1)
    56.             {
    57.                 transform.position = new Vector3(10, 1, -400);
    58.                 transform.Rotate(0, 0, 0);
    59.             }
    60.         }
    61.     }
    62. }
    63. }
     
    Last edited: Oct 28, 2019
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    You got errors in your if statements. single = means assignment, you need to use == (two) to compare objects/values.

    And is this your pseudocode, as there's a lot of other stuff wrong too that will not compile?
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Paste the, if not the whole script, at least the exact script. There's quite a few things in the code (lowercase "o" in "collision.gameObject", = instead of == in the if clause, etc) as written here that would be errors if written in the actual code, which is making it harder to find the actual problem. And always paste the exact error, including the line number, which will point to the actual problem.

    When you need help solving syntax problems, every single character is important, so don't try and paraphrase your code, as that will almost always obscure the the real problem.
     
  4. AnKOu

    AnKOu

    Joined:
    Aug 30, 2013
    Posts:
    123
    At what line ?


    Code (CSharp):
    1. if(collision.gameobject = something)
    2.           {
    3.               Console.WriteLine("example");
    4.           }
    This statement won't produce a compile error since it first assign 'something' to 'compare collision.gameobject' and then compare the later against null.
    But this is probably an undesired behaviour
     
  5. kancelarija12

    kancelarija12

    Joined:
    Sep 17, 2019
    Posts:
    15
    here is the whole code i was just asking about the float and i typed the code on my phone so here is the one that isnt typed manually:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class lobbyanddead : MonoBehaviour
    6. {
    7.     public GameObject play;
    8.     public GameObject scp457;
    9.     public GameObject scp008;
    10.     public GameObject scp173;
    11.     public float blink = 4.0f;
    12.     public int health = 100;
    13.     public bool infected = false;
    14.  
    15.     void Update()
    16.     {
    17.         blink -= 1.0f * Time.deltaTime;
    18.         if(blink <= 0.9f)
    19.         {
    20.             blink = 4.0f;
    21.         }
    22.         if (health <= 1);
    23.         {
    24.             health = 100;
    25.             transform.position = new Vector3(10, 1, -400);
    26.             transform.Rotate(0, 0, 0);
    27.         }
    28.         if(infected == true)
    29.         {
    30.             health -= 1 * Time.deltaTime;
    31.         }
    32.     }
    33.  
    34.     void OnCollisionEnter(Collision collision)
    35.     {
    36.         if (collision.gameObject == play)
    37.         {
    38.             transform.position = new Vector3(22, 1, -69);
    39.             transform.Rotate(0, 0, 0);
    40.         }
    41.         else if (collision.gameObject == scp457)
    42.         {
    43.             transform.position = new Vector3(10, 1, -400);
    44.             transform.Rotate(0, 0, 0);
    45.         }
    46.         else if (collision.gameObject == scp008)
    47.         {
    48.             health -= 10;
    49.             infected = true;
    50.             transform.Translate(Vector3.forward * Time.deltaTime * 10);
    51.         }
    52.         else if (collision.gameObject == scp173)
    53.         {
    54.             if(blink <= 1)
    55.             {
    56.                 transform.position = new Vector3(10, 1, -400);
    57.                 transform.Rotate(0, 0, 0);
    58.             }
    59.         }
    60.     }
    61. }
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Make health a float and it will probably work.

    You're currently subtracting Time.deltaTime from health on line 30. Because health is an int, it doesn't have anywhere to store those fractions of numbers, so the data would be lost. The compiler is basically just checking to make sure that that loss is what you intended (and it's clearly not, in this case).
     
    kancelarija12 likes this.
  7. kancelarija12

    kancelarija12

    Joined:
    Sep 17, 2019
    Posts:
    15
    Thank you it worked!!!
     
  8. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Why are you multiplying by 1?