- Home /
Get reference to persistent object without inspector.
So I have a persistent object called Time. I want other objects(prefabs) to have access to its variables but the other objects wont be persistent, I have hundreds(maybe even thousands) of objects that need to access it I cant assign them through the inspector because they are not instantiated yet(and may likely be destroyed or moved alot) and I doubt using gameobject.find is a good idea.
I keep seeing "if your going to use an object alot keep references in those objects", but how do I do that without assigning in the inspector?
Answer by tanoshimi · Mar 27, 2015 at 04:39 PM
Use a singleton pattern for your Time class, making it publicly accessible to all gameobjects in the scene: http://unitypatterns.com/singletons/
Answer by Neamtzu · Mar 27, 2015 at 03:21 PM
You can make a manager where you can assign this object (drag and drop in the inspector). In this manager get the script component of your Time object and make it static. When you need to access the time component, just call Manager.timescript.variable. Be aware that all the variables / methods you want to access should be marked as public. Another solution will be to make inside your Time script a public static TimeScript Instance, assignt it in start and then access it like this: Time.Instance.variable.
Answer by calabi · Mar 28, 2015 at 05:05 PM
Thanks for the replies I recently discovered singletons that seems to be what I want, if I have the Time object as a static class that wouldn't work would it? Because I have variables constantly being updated.
Your answer
![](https://koobas.hobune.stream/wayback/20220613191326im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
After update of Unity Editor all objects in scene lost references to any scene object. 2 Answers
How to get a referance to an instantiated prefab clone ? 1 Answer
Exposing Variable and Enforce Component Dependencies. 1 Answer
Destroy Child Object(s) OnValidate? 6 Answers
How do you reference different cameras 2 Answers