Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Greg 4 · Mar 15, 2011 at 03:00 PM · c#androidandroidplugincastcs1061

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 fortoString' and no extension method toString' of typefloat' 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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Greg 4 · Mar 15, 2011 at 03:14 PM 0
Share

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!

avatar image Eric5h5 · Mar 15, 2011 at 03:26 PM 1
Share

@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.

avatar image Statement · Mar 15, 2011 at 05:06 PM 0
Share

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.

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How to write a java lib for unity as a plugin to make run another android app? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

calling java method from unity c# 1 Answer

Rendering Android native view with OpenGL 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges