I have a standalone class called “ShipAttrib” whereby I store all the typical variables like HP and Speed of all my player and enemy objects in the game and I paste that class script in all of them. I go around accessing those variables through GetComponent, and since they’re live variables I usually call GetComponent in Update() for quite a number of concurrent game objects simultaneously. I read that that is bad practice. If so, is there a way to get the real time variables without using static variables? Also is there any difference between these 2 codes:
public TextMeshProUGUI speeddisplay;
GameObject Player;
ShipAttrib speed;
void Start()
{
Player = GameObject.FindGameObjectWithTag("Player");
speed = Player.GetComponent<ShipAttrib>();
}
void Update()
{
speeddisplay.text = "Speed: " + speed.Speed;
}
2nd:
public TextMeshProUGUI speeddisplay;
GameObject Player;
void Start()
{
Player = GameObject.FindGameObjectWithTag("Player");
}
void Update()
{
speeddisplay.text = "Speed: " + Player.GetComponent<ShipAttrib>().Speed;
}
}