- Home /
Using a Master Control Script w/Instantiated prefabs & multiple scenes?
I've dabbled in Unity with several mini-projects, and am now attempting something with a little more structure. I'm realizing that I don't know the best way to set up a Master Control script that will work with instantiated objects and in multiple scenes.
My project has two scenes - one which is an empty game map, and another which is a warehouse where my prefabs live.
My game has turret towers which the player can place (instantiate as prefabs) around the map. Each of these towers has a script for tracking the mouse and firing at a target/clicked object.
I'm using a master control script assigned to a dummy object in the scene to handle the raycast for targeting, as well as a boolean which toggles whether turrets should be firing or not. This is also where I'll later track things like game states or score, etc. This seems like a good idea for performance and organizational reasons.
The turrets have their own script, with a variable which points at the Master Control script. This works great if I pre-place the turret in my scene and manually point that variable at a dummy with the MC Script on it. When I try to make this systemic, however, I run into two problems:
1) I don't know where Master Control Script should actually live. As mentioned: I have it currently on a dummy object in the scene, and that script includes DontDestroyOnLoad(transform.gameObject); in the Awake() function, but I'm not sure this actually allows me to have one script which will persist across all scenes. Right now I have to pre-place my turrets in each scene, as well as place a dummy with the MCS in each scene (and point each turret script to that dummy).
2) Probably related to the above, I cannot assign the variable on my turret prefab to point at the object with the MCS.
I think this boils down to me not quite understanding the best way to use prefabs. Would love to hear some input on how to best structure things.
"Right now I have to pre-place my turrets in each scene..." etc.
FWIW that sounds utterly normal and good.
"I think this boils down to me not quite understanding the best way to use prefabs" just purely FWIW I dislike prefabs and think they are silly, confusing and unhelpful. They're greatest value is nothing more than a convenient way to move large groups of stuff from the art department to the program$$anonymous$$g department, ie, they help you figure out "what is used" on some object. So if you don't like prefabs, "give them the arse over the side," as they say on fishing boats in the North!!
Your answer
![](https://koobas.hobune.stream/wayback/20220613072542im_/https://answers.unity.com/themes/thub/images/avi.jpg)