Search Unity

Keeping a list of floats with Javascript

Discussion in 'Scripting' started by Dougxing, Mar 20, 2008.

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

    Dougxing

    Joined:
    Mar 13, 2008
    Posts:
    90
    I need to keep track of several float values that I want to determine at runtime: the intensities of various lights in the scene. I haven't been able to get anything working using either a float[] or an Array datastructure.
    I think the closest I've come is using an Array, but when I retrieve an item it's an Object and not a float, so I can't do math on it. Are you supposed to typecast in javascript? If so, how?
    These things that should be so simple are really frustrating.
     
  2. Mr. Animator

    Mr. Animator

    Joined:
    Oct 2, 2005
    Posts:
    193
    Here's a link to the documentation on array's.

    http://unity3d.com/support/documentation/ScriptReference/Array.html

    And here's some javascript that does what you're describing, making a new array, adding two floats to it, and then printing the sum of the two.

    Code (csharp):
    1. var myArray = new Array ();
    2. myArray.Push(8.7);
    3. myArray.Push(3.9);
    4. print((myArray[0] + myArray[1]).ToString());
    5.  
     
  3. Dougxing

    Dougxing

    Joined:
    Mar 13, 2008
    Posts:
    90
    Aw, sorry for not checking the docs first. I have it working now. I've been using too many different languages lately and I was mixing them up. Plus there's more than one way to do it even in just javascript. Oy.
     
  4. TruffelsAndOranges

    TruffelsAndOranges

    Joined:
    Nov 9, 2014
    Posts:
    92
    How do I grab items from the array and use them in some float calculations? For me it just keeps shouting that I cannot do that because a float is not compatible with an object.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
Thread Status:
Not open for further replies.