Hello people, I have a question which can be related to script optimization. So, in the game, I have a one huge scene. Lets say that gameplay takes up to 30-45 mins. So I created a Level Manager by using Singleton Method, in order to use on the further scenes. The thing is, I have a lot of gameplay scripts, interactions, puzzles, animations, switches etc. I also have a lot of transform hierarchy on my player like camera animator, hand animator, the object that holds the camera script, the camera itself(object with the camera component and image effects) and a lot more like this. As I said, I have a lot of gameplay scripts that need to reach to these game objects on the player. For example, player is able to interact with a lever, when interacted, a script comes in, reaches the stored script called IS_CameraScript, disables it, reaches to the stored game object called “CameraScript” which holds the IS_CameraScript, and lerps the rotation of the object to a certain value, and then reaches to hand object which has Animator on it and plays the hand animations on the lever. So, I have lots of interactions like that. To achieve those, I scripted my storing like this :
IS_LevelManager code, has singleton method on it.
private Transform t_Player;
private Transform t_PlayerHolder;
private Transform t_PlayerCamera;
private Transform t_PlayerChildReference;
private Transform t_PlayerChildRefCrouch;
private Transform t_CameraHolder;
private Transform t_CameraScript;
private Transform t_CameraBaseParent;
private Transform t_CameraAnimator;
// Below these I have Property Methods, I didn't write all of them over here to prevent the code to be messy, but all of those private Transforms has public property methods like the one below.
public Transform T_Player
{
get { return t_Player; }
}
void Awake()
{
RegisterObjects();
}
public void RegisterObjects()
{
//Application.targetFrameRate = 60;
t_Player = GameObject.FindGameObjectWithTag("Player").transform;
t_PlayerHolder = GameObject.FindGameObjectWithTag("PlayerHolder").transform;
t_PlayerCamera = GameObject.FindGameObjectWithTag("PlayerCamera").transform;
t_PlayerChildReference = GameObject.FindGameObjectWithTag("PlayerChildRef").transform;
t_PlayerChildRefCrouch = GameObject.FindGameObjectWithTag("PlayerChildRefCrouch").transform;
t_CameraHolder = GameObject.FindGameObjectWithTag("CameraHolder").transform;
t_CameraScript = GameObject.FindGameObjectWithTag("CameraScript").transform;
t_CameraBaseParent = GameObject.FindGameObjectWithTag("CameraBaseParent").transform;
t_CameraAnimator = GameObject.FindGameObjectWithTag("CameraAnimator").transform;
t_Legs = GameObject.FindGameObjectWithTag("PlayerLegs").transform;
//t_Legs.gameObject.SetActive(false);
cc_Controller = t_Player.GetComponent<CharacterController>();
IS_PlayerController = t_Player.GetComponent<IS_PlayerController>();
IS_CameraController = t_CameraScript.GetComponent<IS_CameraController>();
IS_BobManager = t_CameraHolder.GetComponent<IS_BobManager>();
//IS_ObtainableItem = GameObject.FindGameObjectWithTag("ItemManager").GetComponent<IS_ObtainableItem>();
//IS_WheelInventoryNavigation = GameObject.FindGameObjectWithTag("InventoryCanvas").GetComponent<IS_WheelInventoryNavigation>();
v3_PlayerVelocity = cc_Controller.velocity;
cam_Player = t_PlayerCamera.GetComponent<Camera>();
}
So as you can see, I am storing the transforms like this. When I want to reach & use them, I do this:
One of the gameplay scripts:
private Transform t_Player;
void Start()
{
t_Player = IS_LevelManager.instance.T_Player;
}
void MovePlayer()
{
t_Player.position = ......
}
So what I am asking is, is this the right way to do what I need to do? Is there any better way, I believe like using GameObject.Find… is a very slow way, but since I am doing it on the Awake I wouldn’t have much of a problem, but I am still not really sure. Thanks