Very Low FPS

Hi!

I have very very low fps in my game. My creatures have behaviour script attached and if i remove this script or at least Update() functions fps will become normal.

So suggest me please something to fix in my code to make fps higher!

  // Update is called once per frame
    void Update()
    {
        if (gameObject.transform.rotation != spawnRotation)
        {
            //this.gameObject.transform.rotation = Quaternion.AngleAxis(rotOffset, Vector3.up);
        }

        if (networkView.isMine)
        {
            if (target != null && isAlive && startAggro && target.GetComponent<Player>().isAlive)
            {
                Attack(target);
            }
        }

        Die();

        curHP = Mathf.Clamp(curHP, 0, maxHP);

        if (isAlive)
        {
            if (checkForReAggro && attackersList.Count > 1)
            {
                StartCoroutine(AggroDontHitRecovery(attackersList.Last()));

                if (toggleToOtherTarget)
                {
                    AdjustTarget(attackersList.Last());
                    toggleToOtherTarget = false;
                }
            }

            if (_bUpdateRestoreHpAfterRunaway)
            {
                StartCoroutine(WaitAfterRunAwayForRestore());
                _bUpdateRestoreHpAfterRunaway = false;
            }

            else
            {
                _bUpdateRestoreHpAfterRunaway = false;
            }
        }
    }

    void LateUpdate()
    {
        if (_allowWaypointMove && isAlive && GameObject.Find("MainCamera").GetComponent<Server>().connected) //!It used to be in FixedUpdate 11.01.15 / 0:12:00
        {
            WaypointMovement();
        }
    }

    void FixedUpdate()
    {

        if (isAlive)
        {
            Aggro();
        }

        if (networkView.isMine)
        {
            networkView.RPC("AdjustThePos", RPCMode.AllBuffered, this.transform.position);
            networkView.RPC("AdjustTheRot", RPCMode.AllBuffered, this.transform.rotation);
        }

        if (GameObject.Find("MainCamera").GetComponent<Server>().connected && this.gameObject != null)
        {
            if (isAlive)
            {
                networkView.RPC("AdjustSpeed", RPCMode.All, (transform.position - lastPos).magnitude);
                networkView.RPC("SendFloat", RPCMode.All, "Speed", speed);
            }
        }

        if (target != null)
        {
            if (!(target.GetComponent<Player>().isAlive))
            {
                networkView.RPC("KilledTarget", RPCMode.All);
            }
        }
    }

And WayPoint movement:

  public void WaypointMovement()
    {
        if (networkView.isMine)
        {
            wayPointMovementTime += Time.deltaTime; //current?

            if (_flagWayPoint1)
            {
                wayPointRot = Random.Range(0, 360);

                transform.eulerAngles = new Vector3(0, wayPointRot, 0);
                _wayPointDir = transform.TransformDirection(Vector3.forward); //Skipped
                _timeRadius = Random.Range(0.1f * wayPointMaxRadius, wayPointMaxRadius);

                /*if (networkView.isMine)
                {
                    networkView.RPC("AdjustTheTimeRadius", RPCMode.AllBuffered, (float)Random.Range(0.1f * wayPointMaxRadius, wayPointMaxRadius));
                }*/

                _flagWayPoint1 = false;
            }

            if (!_flagWayPoint1 && wayPointMovementTime < _timeRadius)
            {
                _flagWayPoint2 = true; 
                _wayPointDir.y -= 20.0f * Time.deltaTime; //gravity
                _controller.Move(_wayPointDir * Time.deltaTime * wanderingSpeed);
            }

            else
            {
                if (_flagWayPoint2)
                {
                    StartCoroutine(DoTheWay());
                    _flagWayPoint2 = false;
                }
                //wayPointMovementTime = 0;
            }
        }

        else
        {
            this.gameObject.transform.position = wayPointClientPos;
            this.gameObject.transform.rotation = wayPointClientRot;
        }
    }

    IEnumerator DoTheWay()
    {
        _nextGoTime = Random.Range(2f, 9f);
        /*if (networkView.isMine)
        {
            networkView.RPC("AdjustTheTimeStand", RPCMode.AllBuffered, (float)Random.Range(2f, 9f));
        }*/
        yield return new WaitForSeconds(_nextGoTime);
        _flagWayPoint1 = true;
        wayPointMovementTime = 0;
    }

Hi,

you could begin by removing the Find methods in the LateUpdate() and FixedUpdate() methods, because these methods are traversing the complete scene hierarchy each time if they get called. You call them every frame more than ones, so that could realy be the problem.

In the unity api documentation it says:
“For performance reasons it is recommended to not use this function every frame Instead cache the result in a member variable at startup or use GameObject.FindWithTag.”

Create a public variable and attach the MainCamera to it via the inspector or use the Find-method only in the Start() method to get the reference to your MainCamera.