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 mdagreda · Jan 07, 2013 at 07:23 PM · arrayvariabletype

How do I get unity to tell me what type of variable a variable is?

I need to know how to tell the type of variable a variable is through a script. I am reciving some data from a JSON file and parsing through it. I get back a bunch of variables based on an attribute I choose, but some atrributes return floats and some return ints. I have overloading some functions in order to handle either type but I would like to know how to find the type of variable so I know what kind of arrays to create and fill with the variables. Any ideas?

Comment
Add comment · Show 4
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 Itinerant · Jan 07, 2013 at 07:29 PM 2
Share

It sounds like you need to analyze the data that's co$$anonymous$$g in, then define it based on the string you're getting.

$$anonymous$$y first thought for ints and floats would be to treat it as a string on import. Then do a quick search in the string - if it has a period, convert to float. If no period, convert to int. If it has letters, convert to string.

Granted, there may be times you want to define something that looks like an int as a float, but that's another matter.

avatar image mdagreda · Jan 07, 2013 at 07:32 PM 0
Share

That will probably work. I'll give it a try. thanks.

avatar image mdagreda · Jan 07, 2013 at 07:59 PM 0
Share

How do I do a search? I have trying using the IndexOf method after converting a variable from the dataset into a string but it says there is no method called IndexOf.

avatar image Itinerant · Jan 07, 2013 at 08:09 PM 0
Share

Are you only dealing with ints and floats? If so, my thoughts would be something like this:

function defineType(value : String){

if("." in value){

Debug.Log("Value is a float")

}

else{

Debug.Log("Value is an integer")

}

That said, this will cause issues if you call it for anything but numbers. Do you need to be able to do that, too?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by alexfeature · Jan 07, 2013 at 08:06 PM

Using separators to deduce numeric types is not a good idea. This approach will fail when you get data from a server set up with different regional settings than the client analysing data. So, you would need to check for two symbols ',' and '.' to be sure.

This will introduce unnecessary overhead while processing imported data.

I would recommend using Float.TryParse() first. All numeric values that pass this cast should be treated as floats. If you have other data types such as DateTime you can .TryParse() them too. Any variables failing these casts can be assumed to be Strings.

This is not ideal but unless we are talking about massive amounts of calculations per frame you can ignore ints all together.

Perhaps if you could share a bit more details on what exactly you are receiving a better approach can be found.

EDIT : Forgot the code sample :D

 // In your import function loop
 
 foreach (var value in myJSONValues)
 {
 
 var myFloat = 0F;
 var myDate = (DateTime?)null;
 
 if(float.TryParse(value, out myFloat))
 {
   // myFloat now has a value and you can add it to your Floats array
   continue; 
 }
 
 if(DateTime.TryParse(value, out myDate))
 {
   // myDate now has a value and you can add it to your Dates array
   continue; 
 }
 
 // All casts failed you can treat value as string...which it is anyways :D
 // Add value to Strings array
 
 }


Regards, Alex

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Variable Type for an Instance of any Script 2 Answers

Call array-specific methods on arrays within an array? 1 Answer

Passing a Script Name to a Function 2 Answers

C# - passing a Type var to GameObject.AddComponent 1 Answer

Array of Transform[] not correctly typed in Javascript 1 Answer


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