- Home /
[C#] Accessing A Script From Another Scene?
Im very stuck right now. I have no idea how to approach this. I'm trying to have a settings screen, which its own scene. It will have the option to change the speed variable of the playerMovement script, but the playerMovement script is on another scene. I don't know how to access scripts from other scenes. I searched around a bit but I can't really find anything helpful. If anyone can show me an example I would really appreciate it. Thanks
-Ronnie
You can't directly, you will have to write the data to playerprefs, or to a text file maybe and reread the file in the other scene
Personally, I think its a bad idea to keep settings on a seperate scene, because if the player wanted to change settings during gameplay, they dont really have that option, or increase loading time switching back and forth between scenes and having everything re-spawn and what not, but I dont know your gameplay
Regardless, you dont need to directly access the player$$anonymous$$ovement script - create a namespace, or a script that doesnt dirive from $$anonymous$$onoBehavior - or even a prefab of your player, and a 3rd script to access that prefabbed player - do what you need to do from your Settings scene, and send that info to the 3rd script - since that script would be either a namespace or a script that doesnt derive from $$anonymous$$onoBehaviour, its data would exist in your Projects, rather than on a object - unless you took the prefab route, in which case it would exist on that prefab, and you can access its data with some custom Get and Set functions
$$anonymous$$ake a static class for your settings and access the static variables from your Player$$anonymous$$ovement script.
Answer by Brogan89 · Jun 11, 2017 at 10:18 PM
You can only access script from another scene if it is a static class or a class which has static variables, as far as I know. I wouldn't recommend a new scene for the settings screen however, I would recommend just a UI overlay which uses a singleton model.. I use a completely new Canvas for this and use this, it will not be destroyed. So i know it is present in each scene and from instead of having your options script access the playerMovement script, it would be the other way around. playerMovement accesses settings script.
Your settings script will have something like this:
public class Settings : MonoBehaviour
{
public static Settings instance;
void Awake ()
{
if(instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else if (instance != this)
{
Destroy(gameObject);
}
}
}
And now from your players movement script you could go something like
playerSpeed = Settings.Instance.playerSpeed;
or whatever you want to access. There are probably many other ways to go about this, but this is what I found easiest.
I also suggest watching these videos: https://unity3d.com/learn/tutorials/modules/intermediate/live-training-archive/modal-window?playlist=17111
This is a bit tricky for me, because in my game you can only access the settings screen from my main menu screen, which is also it's own scene. So Im guessing I'll just make a UI overlay for that too right?
Yea i would recommend that too.
I have a canvas which holds my "Window$$anonymous$$anager" script and from. and i'll have an array of all my windows. And each window has a tag, e.g "Options", "Save/Load", "Start$$anonymous$$enu", etc... Then in runtime I can go
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape)) Window$$anonymous$$anager.Open("Options");
This will loop through all my window object until it find "Options" tag.
How you do this is up to you, I use enums. But I followed along that tutorial link i put in. But then changed it to suit my own needs.
Depending on your c# knowledge that is. It isn't really a beginners thing to do. but you'll get there. Go through that tutorial :)
Answer by johnburkert · Oct 16, 2020 at 09:56 PM
You can use scriptable objects to share data across objects and scenes. There are a couple Unity videos about it on their channel.
Your answer
