It seems like there’s more than one way of getting variables of objects and they seem very circumstantial. I’d like to know if I’m doing things correctly and if there are better methods in some of these cases.
Single Global Game Object: Player
public class ScriptName : MonoBehavior {
private PlayerController player;
}
private void Start()
{
player = FindObjectOfType<PlayerController>();
}
private void OnTriggerEnter(Collider other) {
if (player != null)
{
playerBoltDamage = player.shotDamage * powerUp.shot.damage; //would powerUp possibly be defined as a single, global GameObject?
}
}
Is there a better way to go about this? Is this recommended with other objects that are guaranteed to have one instance of? Is there a way I could globally declare somewhere what PlayerController player is so I can simply use player.variable anywhere in any script instead declaring it per script?
Instantiated Object
Should other.CompareTag(“Tag Name”) be used to find a specific object? How do I handle a case where I need to declare more than one kind of tag? In the Space Shooter tutorial, there’s the “Enemy” tag, and two enemy types: the ship and asteroids. I’m uncertain how to call an instance in such a scenario to change a variable.
When a variable of an object is being changed, should the script on that object change the variable, or should an external object’s script change it?
Example:
Collision detected between playerBolt and asteroid
–Should playerBolt’s script tell the asteroid to do something?
or
–Should asteroid’s script do something when it collides with playerBolt?
Or am I confusing myself by going beyond the scope of this tutorial and need to go forward with other tutorials? ![]()
–addon–
Or could I perhaps make a blanket script that works depending on a variable selected?