Hi I am Object Pooling astereroids, but when they suddenly pop up it looks bad. So I have tryied to fade it with lerping transperency, which works but its taking a lot of performance. Calling a function Code (CSharp): void OnTriggerEnter(Collider other) { other.transform.position = GeneratedPosition();// other are Asteriods, Reposition asteriods other.GetComponent<Fade>().Reposition ();//Calling a a component which has every asteriod } Fade Script Code (CSharp): public void Reposition () { fade = true; valToBeLerped = 0; tParam = 0; } // Update is called once per frame void Update () { if(fade){ if (tParam < 1) { tParam += Time.deltaTime * speed; //This will increment tParam based on Time.deltaTime multiplied by a speed multiplier valToBeLerped = Mathf.Lerp(0, 1, tParam); Rend = GetComponent<Renderer>(); color.a =valToBeLerped; Rend.material.color =color; } } } Is there way to optimaze it or do it better? Thanks
It might be better to use animations to do this. Just start at color.a = 0 and set it to 1 after a certain interval. Also, GetComponent is expensive. You should not do that in Update. If you want to keep doing it in code, use a coroutine and create a reference to the Renderer.
Optimisation tips Use coroutines instead of Update Call GetComponent once Keep variables in small scopes Here it is in practice: Code (CSharp): public void Reposition () { StartCoroutine(Fade()); } IEnumerator Update () { Renderer renderer = GetComponent<Renderer>(); Color color = renderer.material.color; float fadeTime = 0; while (fadeTime < 1) { fadeTime += Time.deltaTime * speed; color.a = Mathf.Lerp(0, 1, fadeTime); renderer.material.color = color; yield return null; } } Note that the OnTriggerEnter method belongs on the asteroid, not the player.