Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Public variables not showing up in the inspector and I don't understand why, I'm not getting errors

Discussion in 'Scripting' started by Endode, Jan 28, 2019.

  1. Endode

    Endode

    Joined:
    Jan 13, 2018
    Posts:
    2
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ItemIndex : MonoBehaviour {
    6.  
    7.     public static GameObject errorMesh;
    8.  
    9.     public static GameObject stick;
    10.  
    11.     public static GameObject FindItemWithId(string itemID) {
    12.        
    13.         if (itemID == "stick") {
    14.             return stick;
    15.         } else if(itemID=="") {
    16.             return errorMesh;
    17.         } else if(itemID==" ") {
    18.             return errorMesh;
    19.         } else if (itemID==null) {
    20.             return errorMesh;
    21.         }
    22.  
    23.         return errorMesh;
    24.     }
    25.  
    26. }
    27.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    Because they are static.
     
  3. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    To elaborate, a property marked as "static" makes it behave in a very special way. Instead of each instance of class having its own personal value for a static property, static properties are shared among every instance of the class. It's basically a global variable, with one and only one value.

    To go a step further, you're also trying to write a static method. A static method won't have access to any of the local data about the class. For example, if you had a class called "Person", and each Person had a string property called "FirstName", a static method on Person would not have access to the FirstName property. Usually static methods are used either to create helper methods, and aren't really meaningfully associated with the class they're added to. Static methods are called without even having an instance of an object.

    So, it's probably better to just ask, what are you trying to accomplish that has you relying on static methods and static properties? It doesn't really make sense (or provide any value) for a class having only static methods/properties to derive from MonoBehaviour.
     
    angrypenguin, SparrowGS and Endode like this.
  4. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Did you remove static from the function as well? It will produce a compile error otherwise.
     
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    No errors in the project?
    Scripts wont recompile until all errors are gone
     
  6. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    I think you're still confused about the different between class methods and static methods. You still haven't stated in general what you're trying to accomplish with these methods, but from your first post it seems like you're trying to create some kind of "manager" object that holds on to several objects, and can provide those objects to other classes on request. But using static properties doesn't work for that, as you noticed, because you can't assign static properties in the inspector.

    Two different approach you can take:

    1) You can do what you're doing now, except that you need to create an instance of your ItemIndex class on some gameObject in your scene. I have many "manager" classes like this, which I create as prefabs. In your case, create a new gameObject in your scene, add the ItemIndex script to it, and assign the 'errorMesh' and 'stick' properties to it. Then you can make this object into a prefab and use it in any scenes you want. When you want to access it, instead of trying to call `ItemIndex.FindItemWithId(string)', you'd first call `GameObject.FindObjectOfType<ItemIndex>()' to find the instance, and then call `FindItemWithId' on that. This is a completely viable approach. Again, you're close, you just need to put ItemIndex on an object in the scene, access that object's component.

    2) Another approach is just to make your 'stick' and 'errorMesh' objects into prefabs, and Instantiate them, entirely in code. Then you wouldn't need an ItemIndex class at all.
     
  7. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    So now, because the variables and functions are no longer static, you need an instance of the object.

    Make sure that ItemIndex is attached to exactly one (and only one) object in the scene, then modify it as such:

    Code (CSharp):
    1. public class ItemIndex : MonoBehaviour {
    2.     private static ItemIndex _instance;
    3.     public static ItemIndex instance
    4.     {
    5.         get {
    6.             if (_instance == null || !_instance) _instance = GameObject.FindObjectOfType<ItemIndex>();
    7.             return _instance;
    8.         }
    9.     }
    10.  
    11.     public GameObject errorMesh;
    12.     public GameObject stick;
    13.     public GameObject FindItemWithId(string itemID) {
    14.        
    15.         if (itemID == "stick") {
    16.             return stick;
    17.         } else if(itemID=="") {
    18.             return errorMesh;
    19.         } else if(itemID==" ") {
    20.             return errorMesh;
    21.         } else if (itemID==null) {
    22.             return errorMesh;
    23.         }
    24.  
    25.         return errorMesh;
    26.     }
    27. }
    This adds some helpful static functions to get the one instance. This is called a singleton.

    Then find any place in your code where you are doing

    Code (CSharp):
    1. ItemIndex.FindItemWithId("Blah");
    and replace it with

    Code (CSharp):
    1. ItemIndex.instance.FindItemWithId("Blah");
     
  8. gustavopinent

    gustavopinent

    Joined:
    Jan 14, 2019
    Posts:
    38
    I had the same problem, the error is irrelevant, since there is an error, the editor won't show up the public variables. Once corrected, it will appears.
     
  9. WriterBrandy

    WriterBrandy

    Joined:
    Apr 4, 2020
    Posts:
    2
    I am having a similar problem in my script.

    I am not using any static variables, so the static issue is irrelevant to the case.

    Here is the code in question

    Script 1:

    Store.cs
    float CurrentTime;
    float CurrentTimer = 0f;
    public float StoreTimer = 2f;

    public void GetCurrentTime()
    {
    CurrentTime = CurrentTimer / StoreTimer;
    }

    Script 2
    StoreUIManager.cs


    public Store store;
    public Slider StoreTimerSlider;

    void Awake()
    {
    store = transform.GetComponent<Store>();
    }

    void Start()
    {
    UpdateStoreUI();
    }

    void UpdateStoreTimer()
    {
    StoreTimerSlider.value = Store.GetCurrentTime();
    }

    void UpdateStoreUI()
    {
    UpdateStoreTimer();
    }

    I cannot figure out why it is complaining about the GetCurrentTime method not being able to be referenced.

    If I take the CurrentTimer variables and the StoreTimer variables and set them as public and plug them into the UpdateStoreTimer method rather than just calling the method from the store - which I would prefer to do - I encounter the exact same problem. I'm confused and frustrated.
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Please start a fresh post. Don't hijack some other random post about static public variables when you have no such thing in your code. Besides, it's against forum rules.

    BEFORE you post, please read how to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand errors in general:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/