Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9217753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:42:12+00:00 2026-06-18T02:42:12+00:00

I need to group all my constants into one file and show them on

  • 0

I need to group all my constants into one file and show them on the inspector. Here’s what i’ve tried:

  1. #define constants

    #define speed 10.0f
    #define hp 3
    

    This doesn’t work, no matter where i put them, Error:

    Cannot define or undefine preprocessor symbols after first token in file

  2. Use static

    public static readonly float speed = 10.0f;
    public static readonly int hp = 3;
    

    It works, but when i attach it to the main camera, the constants do not show up in the inspector window. Well now I know inspector doesn’t support static field.

  3. Use Singleton as suggested

    using UnityEngine;
    using System.Collections;
    
    public class GameConfig : MonoBehaviour {
    
    
    private static GameConfig instance;
    
    public GameConfig() 
    {
        if (instance != null) 
        {
            Debug.LogError("GameConfig Warning: unable to create multiple instances");
        }
        instance = this;
    }
    
    public static GameConfig Instance 
    {
        get
        {
            if (instance == null) 
            {
                Debug.Log("GameConfig: Creating an instance");
                new GameConfig();
            }
            return instance;
        }
    }
    

    Now if I add:

    public float speed = 10.0f;
    

    the GameConfig.Instance.speed IS accessible, but the mono editor does not pop out auto completion. And it get this message:

    CompareBaseObjects can only be called from the main thread.
    Constructors and field initializers will be executed from the loading thread when loading a scene.
    Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

    If I try:

    public float speed = 10.0f;
    public float Speed {get {return speed;}}
    

    I get the same message.

But the game can still work, and variables show on inspector correctly.
Note: Even if i fix it, is there any other ways to do? since it seems redundant to write a constants with 2 names (property + field) and tedious work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:42:14+00:00Added an answer on June 18, 2026 at 2:42 am

    To use singleton in a gameObject in Unity by C#, you should not use the constructor of sub class(GameConfig), but create a GameObject and then add the needed component to it. Like this:

    private static GameConfig _instance = null;
    public static GameConfig instance 
    {
        get {
            if (!_instance) {
                //check if an GameConfig is already in the scene
                _instance = FindObjectOfType(typeof(GameConfig)) as GameConfig;
    
                //nope create one
            if (!_instance) {
                var obj = new GameObject("GameConfig");
                DontDestroyOnLoad(obj);
                _instance = obj.AddComponent<GameConfig>();
                }
            }
            return _instance;
        }
    }
    

    By the way, in your method-2, you can get things done by setting up an inspect UI by your self. Build up a custom editor for GameConfig and the add things you want to inspect. Refer to CustomEditor attribute and Editor.OnInspectorGUI for more information. If you don’t know how to customize an inspector or how to extend the default editor, you can find some useful guides in the Extending Editor in Unity’s site (Custom Inspectors section may be suit for you).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to read a 200mb space-separated file line-by-line and collect its contents into
(Python) Given two numbers A and B. I need to find all nested groups
I need to group the data in my indexed array of indexed arrays by
Given the XML below, I need to group by InputItem and sum the costs
I need add a new user group for mediawiki. The new group has more
I need to send email notifications to either a group or user on every
I have got a group of radio buttons that when clicked need to display
I need help with includes in PHP. I have a group of files where
I need to select statements where is fixed post id, group by user id
I have a skewed data set and I need to do a group by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.