Low fps, plz help

Hello guys,
Well I’m making a game and it’s all going well at the start, but after a while the game looses a lot of fps, and the inspector says that this scrypt is one of the “bad guys”, so after some research and many fixing attempts I’m here asking for some advice of you… Here’s my scrypt, its actualy simple but for some reason this “bad boy” is eating alot of resouces.

using UnityEngine;
using System.Collections;

public class Faller : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        Game_Controller Controller = GameObject.FindObjectOfType<Game_Controller>();

        if (Controller.Has_Placed && Controller.IsEating == false || Controller.Has_Placed2 && Controller.IsEating == false)
        {
                CastIT();          
        }
    }

    void CastIT()
    {
        RaycastHit hit;
        Physics.Raycast(transform.position, Vector3.back, out hit;
        
        if (hit.distance >= 0.5f)
        {
            float New_Distance = hit.distance - 0.5f;
            transform.position = new Vector3(transform.position.x, 0, transform.position.z - New_Distance);
        }
    }
}

alt text

I hope someone can give me some advice :slight_smile:

using FindObjectOfType on each update is a bad thing, you really need to cache it first

using UnityEngine;
using System.Collections;

public class Faller : MonoBehaviour
{
	Game_Controller Controller;
	
	// Use this for initialization
	void Start()
	{
		Controller = GameObject.FindObjectOfType<Game_Controller>();      
	}
	
	// Update is called once per frame
	void Update()
	{
		if (!Controller.IsEating && (Controller.Has_Placed || Controller.Has_Placed2))
		CastIT();          
	}
	
	void CastIT()
	{
		RaycastHit hit;
		Physics.Raycast(transform.position, Vector3.back, out hit);
		
		if (hit.distance >= 0.5f)
		{
			//Making new Variable just make GC are more increased.
			transform.position = new Vector3(transform.position.x, 0, transform.position.z - (hit.distance - 0.5f));
		}
	}
}