I am developing a puzzle adventure game, and in short I have a main map with different objects in this map that lead to mini puzzles. I have some same action for every object, but they also do some different things, so I created a different script for same actions.
For example, when the player click an object, a mini-puzzle screen opens, and when mini-puzzle closed, the player teleport to last clicked object. And the background song contunies were it left. The codes for teleportation and background music are ready, but I want this in most optimal way, is there any suggestions?
Here what I did so far:
I had these codes done on my manager script, and objects refer to the manager script. If I attached the script to all objects, and refer to manager in start function, are all the objects execute start function? Or the scipt executed once and all the objects benefit from it?
Here is my code so far for all objects to contunie song and teleport last point and do some other things:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MiniPuzzleScript : MonoBehaviour, IInteractable
{
GameObject data;
GameObject manager;
[SerializeField] GameObject player;
[SerializeField] GameObject onMouseOver;
[SerializeField] GameObject mouseClicked;
public MapName newMapname;
public bool finished;
// Start is called before the first frame update
public enum MapName
{
Vakvak_1_1,
Zodiac_2_1,
Haseki_1_4,
}
void Start()
{
data = GameObject.Find("Load&Save");
manager = GameObject.Find("Manager");
}
private void OnMouseDown()
{
player.GetComponent<PlayerPointAndClick>().clickedItem = gameObject;
player.GetComponent<PlayerPointAndClick>().setDestination(30f);
}
private void OnMouseEnter()
{
if (!finished)
mouseClicked.SetActive(true);
}
private void OnMouseExit()
{
if (player.GetComponent<PlayerPointAndClick>().clickedItem != gameObject && !finished)
{
onMouseOver.SetActive(true);
mouseClicked.SetActive(false);
}
}
public void changeDialogue()
{
//take Item to inventory ? destroy it
}
public void Apply()
{
if(data)
{
//data.GetComponent<LoadData>().loadData.positionOfPlayer.x = GameObject.Find("mandıracı").transform.position.x;
//data.GetComponent<LoadData>().loadData.positionOfPlayer.y = GameObject.Find("mandıracı").transform.position.y;
//data.GetComponent<LoadData>().loadData.positionOfPlayer.z = GameObject.Find("mandıracı").transform.position.z;
manager.GetComponent<Manager>().GetComponent<Manager>().savePosition();
manager.GetComponent<Manager>().saveMusictime();
}
SceneManager.LoadScene(newMapname.ToString());
}
}