Singletons, statics and managers

I’m trying to accomplish something which should be very simple. I have a Controller GameObject with a script attached that instantiates some Prefabs with values determined by fields in the Inspector (e.g. how many Prefabs to instantiate, and where). The Prefab itself has a number of public fields, too, which determine its behaviour. I’d like to have all the values in the same place, on the Controller object.

I’ve read several posts on the subject of singletons and game-state managers, but this seems overkill for what I’m trying to do. I don’t need to handle game-states, I’m just trying to run a small simulation and I’d like to have all the variables available with the minimum of mouse clicks (I’d also like to be able to control them via UI sliders down the road).

I’ve also tried using “inspector” variables for modifying the values which are then assigned to static variables with similar names which can then be accessed from anywhere. This works but seems really convoluted (you double the number of variables in the script and it is just plain ugly looking…)

My question is: are singletons and inspector/static pairs the only two ways of accomplishing this?

Thanks.

I use both of those, depending on the situation… um. However, if you’re really set on wanting all of the data together - do you simply mean on the same object? I can’t be certain if this will work, but have you tried to put a copy of one of the prefab’s scripts on the Controller Object, and then pass it to the instantiated object? (just the one it needs, I mean).
… This assumes I understood what you’re after. :slight_smile:

I think you do understand me, unfortunately I don’t think I understand you :slight_smile: I’m not sure how I would pass a script to an instantiated object (sorry, just starting out…)

Okay, no problem. Well, this presumes that you want each instantiated object to get the same data (if it gets the same script). I’m not 100% sure that it will work, but try to put the script on your Controller Object, and remove it from the prefab. Then, when you instantiate it:

GameObject newobj = Instantiate(prefab);
newobj.AddComponent(GetComponent<NameOfScript>());

Repeating myself, not 100% sure this will work, but I think so. I think the data on the prefab kinda makes more sense, but if it works and you prefer it this way… it’s up to you and not me :slight_smile:

If you didn’t want to use singletons or static variables, then after you instantiate the prefab, pass the reference to the Controller to the script attached to the items in the prefab.
The script on the prefab then stores the reference to the controller and uses it to get information from the controller.

public class Controller : MonoBehaviour
{
//...
  GameObject newobj = Instantiate(prefab);
  newobj.GetComponent<ScriptAttachedToPrefab>().SetControllerReference(this);
}

public class ScriptAttachedToPrefab : MonoBehaviour
{
  Controller controller;

  public void SetControllerReference(Controller _controller)
  {
    controller = _controller;
  }
  Update()
  {
      //Do things 
  }
}

That’s a good idea, too. It was a weird “request” in the OP, that he was hoping to have all of the scripts available on just 1 game object for editing in the inspector. I shared my view that this isn’t really needed, but tried to offer a solution that matched his request, anyways.

Thanks a lot. In the meantime I got by with this on my Prefab:

private MainController settings;

void Awake() {
        settings = GameObject.Find ("MainController").GetComponent<MainController>();
    }

Which does what I need, i.e. set everything up in “MainController” and retrieve the settings with settings.whatever.

I’m still just starting in Unity, for what I need it’s enough :slight_smile: Thanks a lot again!

Ah okay I see. I did misunderstand you a little bit; regardless, you found a solution that works out well for you. Cool & have fun :slight_smile: