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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

C# How do you make this kind of script?

Discussion in 'Scripting' started by KiTS0, Jun 8, 2017.

  1. KiTS0

    KiTS0

    Joined:
    May 25, 2017
    Posts:
    2
    Hello All,


    I am trying to make a script that has the ability to
    define the size of a list in the inspector. And enables
    and disables the MeshRenderer of a Plane by key press
    that's listed inside of that list.

    At the moment I have another script without a
    size list in the inspector working as intended by
    having

    Code (CSharp):
    1. public MeshRenderer mesh;
    2. public MeshRenderer mesh1;
    And so on, for the amount of planes that I have.
    I have six planes attached to a parent. All planes
    have been separted and formed into a cube.

    As you can tell this would become time consuming
    to do without a list in the future.

    Please teach me how to do this, thank you.
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Code (CSharp):
    1. public MeshRenderer[] mesh;
    2.  
    3. //or
    4.  
    5. public List<MeshRenderer> mesh;
    6.  
    Both a list and array can be set in the inspector.
     
  3. Phorsaken

    Phorsaken

    Joined:
    Dec 12, 2013
    Posts:
    27
    If your script is being attached to the parent and there are no other meshrenderd in the object you can get them without having to manually set them in the inspector. Something like this.

    Code (CSharp):
    1. private MeshRenderer[] meshs;
    2. private void Awake()
    3. {
    4.   meshs = GetComponentsInChildren<MeshRenderer>();
    5. }
    You can then loop through this list and do what you need to do without having to manually set anything. Hope this helps.

    -Alexander