Home > OS >  Unity accessing variables from another script performance
Unity accessing variables from another script performance

Time:07-09

Option 1:

public class Player : MonoBehaviour{
    Data _data;
    float speed;

    void Awake(){
        speed = _data.speed;
    }

    void Update(){
        Move(speed);
    }
}

Option 2:

public class Player : MonoBehaviour{
    Data _data;

    void Update(){
        Move(_data.speed);
    }
}

Is there any performance difference between these two using ?

CodePudding user response:

It depends largely if speed in the Data class is a property with some code execution or just a normal field.

not much of a difference:

public float speed = 2f;

has an impact on performance:

public float speed {
    get {
         return ExpensiveFunction();
    }
}
  • Related