You canât create cross scene references when editing scenes, but you can find the script in the scene.
So pretty much every method that finds objects except dragging and dropping them into a field on a MonoBehaviour works. You can GameObject.Find, you can Object.FindObjectOfType, you can have the script youâre looking for register itself to a static variable, or you can get the scene and iterate itâs rootGameObjects
Maybe conception of accessing this variable is actually wrong? Why dont you push a value instead of searching for it ? I donât know why youâd want to do that specifically. Then you can set value before and just pass it through non destroyable object to push it into next scene.
Its a simple prototype i am working on.
What if the value i was searching for was stored as a playerprefs INT. Then i should be able to search for it everywhere rght?
PlayerPrefs has their own methods to get/set values from it:
GetFloat/SetFloat,
GetInt/SetInt,
GetString/SetString.
There is also a method to check is specific key already set - HasKey.
You access those like that :
string someVariable = PlayerPrefs.GetString(âNameOfThePrefâ, âAlternativeValueIfItâsNotSetâ);
You can 100% access a script across scenes during run time, and there is absolutely no need to use player prefs to do so.
First note that you donât have âscriptsâ at run time. You have game objects and their respective components, of which all instances of monobehaviour classes. What youâre looking for is a means to find a reference to, or send/read something to/from the specific instance youâre looking for. And thereâs a few different coding patterns you can use to do so.
Singleton would probably be the most common one that people reach for, though probably isnât always needed. I tend to use the publisher/subscriber pattern, as usually I just want one thing to tell something in another scene to kick something off, or send a bit of information to another scene.
A bit more context with what youâre trying to do would be helpful though.