Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Mrsunnyside11 · May 26, 2020 at 04:11 PM · c#inspectorpublic variableserializefield

Public and SerializedField's not showing in inspector

I was creating a script for my dnd group so we could roll character stats, after writing my script they worked and I could see them in the inspector, but then the next time I opened Unity, my code wouldn't run, it didn't show any compiler errors or anything, just when I clicked play, the button that the code was attached to did nothing.

I soon realized that "[SerializeField]" and "public" do not show up in the inspector, and the objects I was passing into them had no reference.

I had to modify my code to find the objects by name instead, but I was wondering if anyone knew of a solution to the problem, as my next project will grab presets and import them through the inspector.

CODE:

 using System;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 public class RollScript : MonoBehaviour
 {
     public static GameObject gameObjectPanel;
     [SerializeField] static GameObject[] TextGameobject;
     static Text[] texts;
 
     void Start()
     {
         TextGameobject = new GameObject[] { GameObject.Find("Display00"), GameObject.Find("Display01"), GameObject.Find("Display10"), GameObject.Find("Display11"), GameObject.Find("Display20"), GameObject.Find("Display21"), GameObject.Find("Display3") };
         texts = new Text[] { TextGameobject[0].GetComponent<Text>(), TextGameobject[1].GetComponent<Text>(), TextGameobject[2].GetComponent<Text>(), TextGameobject[3].GetComponent<Text>(), TextGameobject[4].GetComponent<Text>(), TextGameobject[5].GetComponent<Text>(), TextGameobject[6].GetComponent<Text>() 
         };
         gameObjectPanel = GameObject.Find("Main_Panel");
     }
 
     public void Exit()
     {
         Application.Quit();
     }
 
     public void ActivateRoller()
     {
         //For testing purposes
         if (Input.GetKey(KeyCode.Z))
         {
             RollGame(76, true);
         }
         else if (Input.GetKey(KeyCode.X))
         {
             RollGame(81, true);
         }
         else if (Input.GetKey(KeyCode.C))
         {
             RollGame(86, true);
         }
         else if (Input.GetKey(KeyCode.V))
         {
             RollGame(91, true);
         }
         //Stack overflow on following numbers
         else if (Input.GetKey(KeyCode.B))
         {
             RollGame(96, true);
         }
         else if (Input.GetKey(KeyCode.N))
         {
             RollGame(101, true);
         }
         else
         {
             RollGame();
         }
         gameObjectPanel.SetActive(false);
     }
 
     static void RollGame(int minValue = 71, bool activated = false)
     {
         List<int> rollz = new List<int>();
         rollz.Clear();
         int wholeTotal = 0;
         bool contains18 = false;
         List<int> rollInt = new List<int>();
         int lowInt;
         if (!activated)
         {
             lowInt = 1;
         }
         else
         {
             lowInt = 2;
         }
         for (int i = 0; i < 6; i++)
         {
             rollInt.Clear();
             for (int r = 0; r < 4; r++)
             {
                 rollInt.Add(UnityEngine.Random.Range(lowInt, 7));
             }
             //foreach (var c in rollInt)
             //{
             //    Debug.Log("<color=red>"+c+"</color>");
             //}
             rollInt.Sort();
             //foreach (var c in rollInt)
             //{
             //    Debug.Log("<color=green>" + c + "</color>");
             //}
             rollInt.RemoveAt(0);
             //foreach (var c in rollInt)
             //{
             //    Debug.Log("<color=blue>" + c + "</color>");
             //}
             int total = 0;
             foreach (var c in rollInt)
             {
                 total += c;
             }
             if (total == 18)
             {
                 contains18 = true;
             }
             rollz.Add(total);
             wholeTotal += total;
         }
         if (wholeTotal < minValue)
         {
             //print("WHOLE TOTAL: " + wholeTotal);
             RollGame(minValue, activated);
         }
         else
         {
             rollz.Sort();
 
             Console.WriteLine("Contains 18: {0}", contains18);
 
             //foreach (var c in rollz)
             //{
             //    print(c);
             //}
             for (int z = 0; z < 6; z++)
             {
                 texts[z].text = rollz[z].ToString("D2");
             }
 
             texts[6].text = wholeTotal.ToString("D3");
         }
 
     }
 
     public void backAction()
     {
         gameObjectPanel.SetActive(true);
     }
 }
 

INSPECTOR: alt text

inspector.jpg (14.6 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ADiSiN · May 27, 2020 at 12:59 AM

Hi!

Did you accidently made them static?

Because the reason you cannot see them in Inspector is because they are static - if you will remove static token you should be able see these variables in the Inspector.


For the information - you cannot see static variable in the Inspector because it's the variable that shared between all instances of the class that are created, so it won't make sense to be able set up it individually per each instance of script as it is acessed by its type, not instance.

You can find an workaround to expose static variables in the Inspector, but it's more like about creating another variable that will be equal static variable and through another variable set up static variable.


Hope it helps.

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

131 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 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

C# | public variables in parent class should not show up in child class 1 Answer

Multiple Cars not working 1 Answer

C# public decimal variable not showing up in inspector 2 Answers

Distribute terrain in zones 3 Answers

How to make public variable read-only during run-time? 3 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