• 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 DocQwerty · May 28, 2018 at 04:48 AM · versiondecimalglobal variable

decimals not allowed to update as global varibles from Unity to C# script?,Why are decimals not allowed to apply as global variable to be changed in Unity?

I am going through a training program in Unity and the person is using float for the variables, while I want to use the more precise decimal. I have worked so far, until he has now gotten to the section of Global variables.

I do like that I can use the objects in Unity Script applied to the object to fill in the value. BUT this does not work if I use decimal. It works for Integers, Floats, and Doubles no problem. but why not Decimals? Is this because only C# 4 is in the compatibility to Unity? My version of Unity is 2018.1.0f2,I am going through a training program in Unity and the person is using float for the variables, while I want to use the more precise decimal. I have worked so far, until he has now gotten to the section of Global variables.

I do like that I can use the objects in Unity Script applied to the object to fill in the value. BUT this does not work if I use decimal. It works for Integers, Floats, and Doubles no problem. but why not Decimals?

Comment
Add comment · Show 1
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 Nerull22 · May 28, 2018 at 04:54 AM 0
Share

I'm sorry, I'm not understanding what you're getting at. Are you trying to store a global variable, or are you saying that you want to use a Decimal value in the editor? I'm not following.

3 Replies

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

Answer by FlaSh-G · May 28, 2018 at 12:02 PM

You're not talking about global variables, but public/serializable fields, are you?

Unity can only serialize a specific set of types:

  • All classes inheriting from UnityEngine.Object, for example GameObject, Component, MonoBehaviour, Texture2D, AnimationClip.

  • All basic data types like int, string, float, bool.

  • Some built-in types like Vector2, Vector3, Vector4, Quaternion, Matrix4x4, Color, Rect, LayerMask.

  • Arrays of a serializable type

  • List of a serializable type)

  • Enums

  • Structs

Source: https://docs.unity3d.com/ScriptReference/SerializeField.html

decimal is not one of them, so it can't be serialized jsut like that. To be honest, I'm not sure how the current state is with adding new types to this all.

Either way - if it's not serializable, it will not be shown in the inspector. I'd recommend thinking through why you want this extreme precision anyways.

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
avatar image
1

Answer by Nerull22 · May 28, 2018 at 04:41 PM

I'm going to answer in regards to Flash-G's answer posted above. I don't disagree with his statement, but I think there are routes that were left out of the answer.


Nothing he said was inaccurate, you can not just serialize a decimal data type without some work. What you can do though, is you can create your own version of decimal and store that. I posted some basic code that can achieve this. You would need to write a custom property drawer to get it to operate the way that you want. This is untested and people should correct me where I'm making false assumptions here, but this is where I would start with the problem.

 using System;
     using System.Globalization;
     using UnityEngine;
 
     [Serializable]
     public class UnityDecimal : ISerializationCallbackReceiver
     {
         private decimal _decimalValue;
 
         [SerializeField]
         private string _storedValue;
 
         public decimal Value
         {
             get
             {
                 return _decimalValue;
             }
             set
             {
                 _decimalValue = value;
             }
         }
 
         public void OnBeforeSerialize()
         {
             _storedValue = Convert.ToString(_decimalValue, CultureInfo.InvariantCulture);
         }
 
         public void OnAfterDeserialize()
         {
             _decimalValue = Convert.ToDecimal(_storedValue);
         }
     }
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 FlaSh-G · May 28, 2018 at 06:05 PM 0
Share

This is an interesting approach. An additional custom editor class would make sure only valid values would be entered, and implicit casts could make it nice to use.

However, I'll still stick with my "Are you sure you even need the extra precision" before I'd implement all this :)

Edit: Oh, and to be able to display and change the proper value in play mode, the editor class would have to do some additional back and forth. At this point, purchasing Odin might also be an option :)

avatar image Nerull22 FlaSh-G · May 28, 2018 at 06:42 PM 0
Share

I agree, there is work to be done here definitely. And should absolutely validate that it's necessary before doing work you don't need. Definitely good asterisk here. :)

avatar image DocQwerty · May 28, 2018 at 10:55 PM 0
Share

Considering I am still new into the C# program$$anonymous$$g I think trying to create a special class would be a bit beyond. I do appreciate the info though!

:)

avatar image
0

Answer by DocQwerty · May 28, 2018 at 10:53 PM

Well the training class did not indicate public/serializable fields, but if that is what they are called, I will forget it by tomorrow. :) Basically they the field "public double BaseStoreCost;" was listed in c# and on the Unity graphics engine you can see it in a box to edit.

the basic program I am learning to build is fine with float/double. but I eventually want to get it to need extremely precise numbers. I tinkered with c and c++ in the past and found how unreliable precision is with float and double. When I found decimal I was happy.

I did figure out a way around my issue though. I used float/double as my basis for the serialized field and converted it to decimal for the final storage.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

84 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

AssemblyInfo or alternative - How to add version number 1 Answer

Can I open a version controlled project with Unity Free? 1 Answer

Download link for previous versions of unity3d (3.0) 1 Answer

Asset Store Prefabs Required Unity Version. 0 Answers

How to check what version of an asset store product you have? 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