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.

Typecasting in Javascript?

Discussion in 'Scripting' started by StarManta, Nov 16, 2006.

  1. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,662
    I can't seem to figure out how to typecast in Unity's Javascript. I've tried:

    Code (csharp):
    1. float(num)
    2. Float(num) <-- the normal way to typecast in JS, according to Google.
    3. (float)num
    4. num.toFloat() and various other toFloat syntaxes
    5.  
    The only method i've found that works is creating a temporary variable:
    Code (csharp):
    1.  
    2. var sx : float = screen.width;
    3.  
    Obviously this makes for much uglier-than-necessary code. How do you typecast in JS?
     
  2. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    That's the only one available at the moment...

    Well apart from this:
    Code (csharp):
    1.  
    2.   (0.0 + screen.width)
    3.  
    Dang! ... placing that edit button next to the quote button is DANGEROUS!
     
  3. slippyd

    slippyd

    Joined:
    Jun 11, 2005
    Posts:
    129
    I'm getting a typecasting problem, but the above solution doesn't seem to work. I'm doing:
    Code (csharp):
    1. var charColliders : Collider[] = GetComponentsInChildren(Collider);
    Thanks in advance.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,662
    GetComponentsInChildren returns Component[]. While Javasccript knows how to cast Component into Collider, apparently it can't do the same for arrays of them. So what you do is:

    Code (csharp):
    1.  
    2. var charColliders : Component[] = GetComponentsInChildren(Collider);
    3. for (c=0;c<charcolliders.length;c++) {
    4. var thisCollider : Collider = charColliders[c];
    5. //whatever you're doing to those colliders
    6. }
    7.  
     
  5. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    any idea how to properly typecast this (Editor Script):

    var esinstance : ExampleScript = (ExampleScript)target;

    Cheers

    AC
     
unityunity