Scripting Problems with var arrays
Hello guys, I really tried finding out what is wrong with my code, I know it's about "arrays" and "var" that is making a lot of garbage collection.
Please help me resolve this.
I tried profiler and this is what it shows about MatSwitch script:
Here is the script itself:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MatSwitch : MonoBehaviour {
public Material mainMat;
public Material newMat;
public Renderer childrens;
public float max;
static float t;
public float thres;
public bool isMatNew;
public bool access;
public static bool zeroSlider;
public void Start()
{
isMatNew = false;
thres = newMat.GetFloat("_Threshold");
}
public void Update () {
Renderer[] childrens;
childrens = GetComponentsInChildren<Renderer> ();
if (Input.GetKeyDown(KeyCode.R) && access == false)
{
isMatNew = false;
}
if (access == true)
{
isMatNew = true;
if (zeroSlider == true)
{
isMatNew = false;
}
}
if (Input.GetKeyUp (KeyCode.R) && access == true)
{
isMatNew = false;
}
if (isMatNew == true)
{
t = 0.75f * Time.time;
thres = Mathf.PingPong(t, max)+ 0.2f;
newMat.SetFloat ("_Threshold", thres);
foreach (Renderer rend in childrens) {
var mats = new Material[rend.materials.Length];
for (var j = 0; j < rend.materials.Length; j++) {
mats [j] = newMat;
}
rend.materials = mats;
}
}
if (isMatNew == false)
{
foreach (Renderer rend in childrens) {
var mainMats = new Material[rend.materials.Length];
for (var k = 0; k < rend.materials.Length; k++) {
mainMats [k] = mainMat;
}
rend.materials = mainMats;
}
}
}
}
Answer by Hellium · Oct 04, 2018 at 12:59 PM
Because your code is not correctly documented and your variables horribly named, nobody wants to read this....
I have made an attempt to optimize a little bit your code, but you may not have the desired results, because I had to udnerstand what your code is doing just by reading it, and as I indicated, it's really hard without any comment and poorly named variables....
using System.Collections;
using UnityEngine;
public class MatSwitch : MonoBehaviour
{
private const string thresholdProperty = "_Threshold";
public static bool zeroSlider;
public Material mainMaterial;
public Material newMaterial;
public float max;
public bool access;
private Renderer childrenRenderers;
private float threshold;
public void Start()
{
isNewMaterial = false;
threshold = newMaterial.GetFloat( thresholdProperty );
childrenRenderers = GetComponentsInChildren<Renderer> ();
}
public void Update ()
{
// Reset to main material
if ( Input.GetKeyDown( KeyCode.R ) )
{
AssignMaterialToChildren( mainMaterial );
}
// Update the new material
else if (access && !zeroSlider)
{
threshold = Mathf.PingPong( 0.75f * Time.time, max ) + 0.2f;
newMaterial.SetFloat ( thresholdProperty, threshold );
AssignMaterialToChildren( newMaterial ) ;
}
}
/// <summary>
/// Assign the given material to the children renderers
///</summary>
private void AssignMaterialToChildren( Material material )
{
for( int rendererIndex = 0 ; rendererIndex < childrenRenderers.Length ; ++childrenRenderers )
{
Material[] materials = childrenRenderers[rendererIndex].materials;
for (int materialIndex = 0 ; materialIndex < materials.Length; ++materialIndex)
materials[materialIndex] = material;
childrenRenderers[rendererIndex] = materials;
}
}
}
Thank you for answering, okay so my script is all about changing the material in all objects attached to main gameobject I will try to look at your version
so my script is all about changing the material in all objects attached to main gameobject
Yes, obviously, I understood that.... But what is access
and zeroSlider
? Which script changes them?
It's goes from the Score script, where are the slider of time and the count of the score, which is giving the access to the $$anonymous$$atSwitch script.