- Home /
float to string c# unity script Android csharp
Should be quite a straightforward question, seems to be getting out of hand...
I have a float that I update from a plugin on an Android Device, I want to print it out to the screen, simple right?
here is my code: (csharp)
private float xValue;
xValue = JNI.CallFloatMethod(JavaClass, JNI.GetMethodID(cls_JavaClass, "getX", "()F"));
String value = xValue; //.toString() // (String)xValue; // Float.parse(xValue)
and I get errors
Assets/CallJavaCode.cs(48,55): error CS1061: Type float' does not contain a definition for
toString' and no extension method toString' of type
float' could be found (are you missing a using directive or an assembly reference?)
I have tried to cast it as a String (String)
I have tried to call the csharp .net .ToString and Float.Parse, Float.TryParse
but it seems float lacks inheritance from a string and does not contain any such type of functions
Now, I am quite confused on this because others have given answers to forum questions saying just use toString(), but I believe Android doesn't have such features (at least .net subset / .net (yes i tried both) )
Documentation seems to be very sparse (maybe I am just spoiled by the depth of MSDN) on what is and isn't included with the .net framework for c# scripting, quite sad really
Answer by Statement · Mar 15, 2011 at 03:07 PM
...but it seems float lacks inheritance from a string...
Actually, ToString is defined in Object, which all types (including int, float etc) derive from. toString doesn't exist, so theres no point attempting that. So you say String value = xValue.ToString(); doesn't work? I find this very odd. Maybe you can try this instead:
String value = String.Format("{0}", xValue);
float.Parse and float.TryParse just attempts to create a float from a string, not the other way around, so it won't do what you want.
I meant to put String.parse not float.parse, and inheritance from object... (been staring at it too long to make sense anymore)
That does work btw, thank you
Also you seemed to have solved the mystery as to WHY it wasn't working
c# it is .toString() you wrote .ToString() gave it a shot and vola! it worked...
Wheres the documentation on this? frustrating when they decide to call their scripts c# or js and in fact its just unityscript based on those languages and they don't tell you the differences!
@morty346: see the $$anonymous$$SDN docs. http://msdn.microsoft.com/en-us/library/system.object.tostring(v=VS.90).aspx It is not ".toString()" and never was. C# in Unity is standard C#, so there are no differences to tell you about. "JS" should be called Unityscript and isn't Javascript, but all the $$anonymous$$ono/.NET functions are standard in any case. $$anonymous$$aybe you're thinking of Java, but Unity doesn't use Java.
Yeah, toString exist in Java (not to be confused with UnityScript/JS. Thats why we refer to it as UnityScript to $$anonymous$$imize confusion). ToString exist in .Net/$$anonymous$$ono and is used in Unity.
Answer by Ashkan_gc · Mar 15, 2011 at 03:11 PM
ToString is a method that all objects have (yes float is an object, in that definition everything that takes space in memory is an object). all classes and object types in .NET are children of system.object and have ToString either implicit or explicit. i mean even if you don't override it for your class it will have a ToString() that returns the name of your class. so the problem is not in .NET/float side. even the smallest versions like .NET microframework have this feature. maybe because you set it from a Native call it changed it's type to a none dotnet, normal float value (i,e 4 bytes of memory and nothing more) and maybe the value of it is something wrong that could not be converted. i think my first assumption is true due to the eror's message. however i never used unity android or JNI calls.