Search Unity

Question Initializing and accessing values in an array of structs

Discussion in 'Scripting' started by dblondin, Dec 6, 2022.

  1. dblondin

    dblondin

    Joined:
    Dec 28, 2021
    Posts:
    19
    I'm trying to initialize an array of structs containing components so that I can assign them as references for gameObjects. The hope is that each element of the struct array will point to each gameObject's components (and whatever values I need to store for each gameObject, probably a lot of ints and floats). This is what my brain is telling me to do but it seems wrong as I'm not able to access contents of the struct array. Though I am able to access things in the struct that I'm building the array with.

    Code (CSharp):
    1. public struct Components
    2. {
    3.     public static Rigidbody2D rigidBody;
    4.     public static BoxCollider2D boxCollider;
    5.     public static SpriteRenderer spriteRenderer;
    6. }
    7.  
    8. public Components[] ComponentsArray = new Components[50];
    What am I doing wrong, what would be the best way to achieve this?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I'm not sure I understood what you meant, but all the values of your struct are static. Are you sure you don't want non-static values? Are you just trying to have a variable on a monobehaviour script that you can store info about the gameobject in?
     
  3. dblondin

    dblondin

    Joined:
    Dec 28, 2021
    Posts:
    19
    Ah thank you, that has fixed my issue. I have been adding static to my struct variables and have not considered what it's doing - repeating what's known to work in past cases.

    But it looks like I need to add static here to make the values available in other scripts?

    Code (CSharp):
    1. public struct Components
    2. {
    3.     public Rigidbody2D rb;
    4.     public BoxCollider2D bc;
    5.     public SpriteRenderer rend;
    6.     public int test;
    7. }
    8.  
    9. public static Components[] ComponentsArray = new Components[50];
    10.  
    11. //...
    12.  
    13. private void functionInAnotherScript()
    14. {
    15.     ComponentsArray[5].rb.isKinematic = true;
    16. }
     
  4. dblondin

    dblondin

    Joined:
    Dec 28, 2021
    Posts:
    19
    And yes "trying to have a variable on a monobehaviour script that you can store info about the gameobject in" is the goal.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Right away this breaks encapsulation. Why should "another script" know anything about the details of how stuff is stored somewhere else?

    If an external script needs to do something like "hey you, stop processing physics!" then that should be your API, such as:

    OtherObject.StopUsingPhysics();


    Then inside of the OtherObject you would have an instance method called StopUsingPhysics() that would set its isKinematic to true.

    Otherwise, madness... spaghetti!

    The general blurb:

    Referencing variables, fields, methods (anything non-static) in other script instances:

    https://forum.unity.com/threads/hel...-vars-in-another-script.1076825/#post-6944639

    https://forum.unity.com/threads/accessing-a-gameobject-in-different-scene.1103239/

    REMEMBER: it isn't always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.

    Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

    That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.
     
  6. dblondin

    dblondin

    Joined:
    Dec 28, 2021
    Posts:
    19
    Excellent. I agree I've not fully considered the benefits of encapsulation. I've been struggling with some of this as my year long project continues to grow. This is a perfect next step. Thank you!