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

Array is Not Returning It's Entire Length

Discussion in 'Scripting' started by FantasticDamage, Feb 3, 2020.

  1. FantasticDamage

    FantasticDamage

    Joined:
    May 14, 2017
    Posts:
    18
    I'm trying to get an array of all of the materials on a gameObject when the scene starts. I have a function called GetOriginalMaterial() that will find the Renderer component in each of the children gameobjects and get the materials. For each of the materials found, they will be stored in a variable called originalMaterials for later use.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyControllerCopy : MonoBehaviour
    6. {
    7.  
    8.     //variable for the Renderer array
    9.     [SerializeField]
    10.     private Renderer[] skinMeshChildren;
    11.     //variable for the original materials
    12.     [SerializeField]
    13.     private Material[] originalMaterials;
    14.  
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.      
    20.         //get the renderer components of this gameobject's children
    21.         skinMeshChildren = GetComponentsInChildren<Renderer>();
    22.         //upon starting the scene, get the game object's materials that it started with
    23.         GetOriginalMaterial();
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.      
    30.     }
    31.  
    32.     private void GetOriginalMaterial()
    33.     {
    34.         //for each of the Renderer components in the children
    35.         foreach (Renderer rend in skinMeshChildren)
    36.         {  
    37.             //get the total number of materials that is in the Skinned Mesh Renderer in the children
    38.             //any material found in the children is going to be the original material that the gameobject started with
    39.             for (int i = 0; i < rend.materials.Length; i++)
    40.             {
    41.                 originalMaterials = rend.materials;
    42.             }
    43.  
    44.         }
    45.     }
    46. }
    47.  
    When I start the scene however, I don't get all of the materials from the child game objects, and that is the current problem that I am trying to solve.


    Here we see that there are 3 materials on this game object called Body_Material, Body_Plastic and Body_vinyl.


    However when the scene starts, the script is only able to grab 2 materials. What happend to Body_vinyl?
     

    Attached Files:

  2. PaulR

    PaulR

    Joined:
    Nov 14, 2012
    Posts:
    43
    Code (CSharp):
    1. originalMaterials = rend.materials;
    You're just overwriting your materials array each time.
    Try using a List<> and doing AddRange