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

Cannot convert 'UnityEngine.gameObject' to 'float'

Discussion in 'Scripting' started by kjartanith, Jul 27, 2011.

  1. kjartanith

    kjartanith

    Joined:
    Jul 22, 2011
    Posts:
    25
    I swiched over to unity 3.4.0 and it came up with this error:

    Cannot convert 'UnityEngine.gameObject' to 'float'

    I don´t know how to fix this.

    Code (csharp):
    1.  
    2. var waterLevel : float;
    3. var uAudio : AudioClip;
    4. var aAudio : AudioClip;
    5.  
    6. var uColor = Color(1,1,1,1);
    7. var uDensity = .05;
    8.  
    9. var aColor = Color(1,1,1,1);
    10. var aDensity = .008;
    11.  
    12. var waterSurface : Renderer;
    13. var underwaterSurface : Renderer;
    14.  
    15. private var below = false;
    16. private var glow : GlowEffect;
    17. private var blur : BlurEffect;
    18.  
    19.  
    20. function Awake() {
    21.     if(!waterLevel)
    22.     {
    23.         water = FindObjectOfType(Water);
    24. [COLOR="Blue"]                if(water) waterLevel = water.gameObject;[/COLOR]
    25.     }
    26.     aColor = RenderSettings.fogColor;
    27.     aDensity = RenderSettings.fogDensity;
    28.    
    29.     glow = GetComponent(GlowEffect);
    30.     blur = GetComponent(BlurEffect);
    31.     if( !glow || !blur )
    32.     {
    33.         //Debug.LogError("no right Glow/Blur assigned to camera!");
    34.         enabled = false;
    35.     }
    36.     if( !waterSurface || !underwaterSurface )
    37.     {
    38.         //Debug.LogError("assign water  underwater surfaces");
    39.         enabled = false;
    40.     }
    41.     if( underwaterSurface != null )
    42.         underwaterSurface.enabled = false; // initially underwater is disabled
    43. }
    44.  
    45. function Update ()
    46. {
    47.     if (waterLevel < transform.position.y  below)
    48.     {
    49.         audio.clip = aAudio;
    50.         audio.Play();
    51.         RenderSettings.fogDensity = aDensity;
    52.         RenderSettings.fogColor = aColor;
    53.        
    54.         below = false;
    55.        
    56.         glow.enabled = !below;
    57.         blur.enabled = below;
    58.         waterSurface.enabled = true;
    59.         underwaterSurface.enabled = false;
    60.     }
    61.    
    62.     if (waterLevel > transform.position.y  !below)
    63.     {
    64.         audio.clip = uAudio;
    65.         audio.Play();
    66.         RenderSettings.fogDensity = uDensity;
    67.         RenderSettings.fogColor = uColor;
    68.        
    69.         below = true;
    70.        
    71.         glow.enabled = !below;
    72.         blur.enabled = below;
    73.         waterSurface.enabled = false;
    74.         underwaterSurface.enabled = false;
    75.     }
    76. }
    77.  
    78.  
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,753
    waterLevel is a float
    water.gameObject is a GameObject

    They don't match.

    if(water) waterLevel = water.gameObject.transform.position.y;

    might be what you're after.

    -edit-

    fixed code (thanks Afisicos)
     
    Last edited: Jul 27, 2011
    Azurne likes this.
  3. Afisicos

    Afisicos

    Joined:
    Nov 13, 2010
    Posts:
    326
    you can't asing a gameObject to a float variable. waterLevel is a float.

    yes. tertle said the correct code, but is water.gameObject.transform.position.y
     
  4. kjartanith

    kjartanith

    Joined:
    Jul 22, 2011
    Posts:
    25
    Thanks. But this is weird i opened the old unity and the new one again, and the error disappeared
     
    Last edited: Jul 27, 2011
  5. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,249
    I actually got this error too when moving to 3.4 only IOS build (not on standalone).

    Haven't figure how to get rid of it yet!
     
  6. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,249
    In the end i figured my issue was I took the shortcut

    var myNumber = 0.0; as a cheat in javascript

    but when i changed it to var myNumber : float = 0.0; it didn't have the issue. Seemed to be picking it up as an object because I didn't explicitly declare it.
     
  7. boco

    boco

    Joined:
    Dec 7, 2010
    Posts:
    373
    look at this
    this set waterLevel to a float variable now look what your trying to assign to it

    you can see it does not match what you want to do is this
    so whuddanaim got it right...
     
  8. Kashrlyyk

    Kashrlyyk

    Joined:
    Aug 16, 2011
    Posts:
    17
    I just want to point out that not the OP made that error in the code, but whoever wrote the "IslandDemo" or whoever wrote the 3.4 version of Unity. It is weird to see official demos not working anymore with new versions of the program.
     
  9. MrBurns

    MrBurns

    Joined:
    Aug 16, 2011
    Posts:
    378
    Sadly, that is not really true with Unity. And I have also posted a wishlist item for this. Internally Unity has an implicit bool cast for UnityEngine.Object and whatsoever. Since I am not familiar with Unity Script I only know from real JavaScript that converting bool into float should be possible. So I would say that this type conversion could have worked in Unity 3.3 by doing Object->Bool->Float...
    But since 3.4 they made typing stricter in US, so that this will fail now, I suppose...

    BTW, this is terrible coding practice anyway, so its at least some sort of improvement that this fails now!
     
  10. ramp

    ramp

    Joined:
    Nov 6, 2012
    Posts:
    14
    Hi,

    You can use the following code instead of code line 23.// if(water) waterLevel = water.gameObject;

    if (water) waterLevel = water.transform.position.y;

    Thanks
    Ram