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

Question Referencing a static array and serializing a member from another class in the inspector

Discussion in 'Scripting' started by Darkadin, Sep 13, 2023.

  1. Darkadin

    Darkadin

    Joined:
    Oct 28, 2022
    Posts:
    8
    ok, so Im going to attempt to describe what I mean. I have a platformer game like super mario world. The overworld is a separate scene than my levels. I need a way to keep track of which levels have been completed.

    So far, I feel i am close but also far. I have a static class with an array of bools. Each one will be set to false.

    What I need is a way to reference that static classes array.

    I want to have an object called Node, that is a prefab, that has a script with a serialized int, level. Then I want to write something along the lines of...

    Code (CSharp):
    1.     [SerializeField] int level;
    2.     LevelTracker levels;
    3.  
    4.  
    5.     // Start is called before the first frame update
    6.     void Start()
    7.     {
    8.         spriteRenderer = GetComponent<SpriteRenderer>();
    9.  
    10.         if (levels.level)
    11.         {
    12.             spriteRenderer.color = Color.green;
    13.         }
    14.         //else
    15.         {
    16.             //spriteRenderer.color = Color.red;
    17.         }
    18.     }
    19. }
    For reference, LevelTracker is my static class with my array of bools.

    I guess what I am trying to do, is reference an array from a static class and specifically use the inspector from my gameobject from another class to pick out the bool from my array and check if it is true or not.

    The problem of course is when I use {variable . variable} it cant distinguish that I want the first part from one class and the second part from another class.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,006
    Well you can't serialise static anything. Static values only live in memory.

    If you want to serialise something, you need an actual instance of it.

    If you have a static class with an array, just access it like you would anything else static.
     
  3. Darkadin

    Darkadin

    Joined:
    Oct 28, 2022
    Posts:
    8
    I want to access one bool within my static array. Instead of writing a bunch of scripts for each level, I want my level gameobject to serialize which bool from my array i am choosing to reference.

    Let's say I beat level 1 and return to the world map.

    My level 1 game object is going to reference the static class and the bool array within.
    Which bool do i want?
    I want to be able to change that in the inspector so that each level prefab can reference a different bool within the array.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,006
    I guess the short of it is it isn't going to work the way you want it to.

    The most you can do is write an Editor or PropertyDrawer that turns the index into a dropdown that lets you select from an index of this static array, and at runtime you pull the data out of it. But you can't specifically serialise a reference to anything static, particularly not a value type within an array.

    What's the actual end goal here? It sounds like you should be using scriptable objects to outline per-level information, honestly.
     
  5. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    If levels can only be accessed in logic order ( 1,2,3 and so on), why not keep track of the highest level reached only. Whenever you need to check levels, or list levels and turn the completed ones green, you can simply check for "lower than highest reached".
     
  6. Darkadin

    Darkadin

    Joined:
    Oct 28, 2022
    Posts:
    8
    Thats how I originally had it but then I realized I wanted the player to be able to access all the levels at the start and to go back and forth between them. So when I beat the second level first, it registered as level 1 having been beat because of the highest level reached statement i had.
     
  7. Darkadin

    Darkadin

    Joined:
    Oct 28, 2022
    Posts:
    8
    my end goal is for prefab objects in my level select scene to at the start of the level reference whether i have beaten that level before. All in one script so i can just plug these prefabs in and change the level number in the inspector.
     
  8. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    So basically each index of that array should line up with your level count(level 1 = [0], level 15 = [14], etc..). You just need to add that many bools to how many levels you have.

    and to get, or modify, would simply be:
    Code (CSharp):
    1. if (levels.levelArray[14]) // level 15 is true/completed
    2. {
    3.     spriteRenderer.color = Color.green;
    4. }
     
  9. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,006
    Sounds like a bit of an over simplified save-game system. I would really look to use scriptable objects to properly encapsulate level information or to broadcast information between scenes. Then you can easily reference these objects without any custom editor tools.

    Trying to reference static values is going to end up breaking rather easily.