Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RonnieThePotatoDEV · Jun 11, 2017 at 03:57 PM · c#sceneaccessing scripts

[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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShadyProductions · Jun 11, 2017 at 04:07 PM 0
Share

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

avatar image Dibbie · Jun 11, 2017 at 04:09 PM 0
Share

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

avatar image sleepandpancakes · Jun 11, 2017 at 05:26 PM 0
Share

$$anonymous$$ake a static class for your settings and access the static variables from your Player$$anonymous$$ovement script.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RonnieThePotatoDEV · Jun 12, 2017 at 03:25 PM 0
Share

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?

avatar image Brogan89 · Jun 13, 2017 at 03:31 AM 0
Share

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.

avatar image Brogan89 · Jun 13, 2017 at 03:32 AM 0
Share

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 :)

avatar image
0

Answer by francouzdy9 · Oct 16, 2020 at 08:58 PM

You could always try making a prefab

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

https://www.youtube.com/watch?v=6vmRwLYWNRo

https://www.youtube.com/watch?v=raQ3iHhE_Kk

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

333 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to make buttons have sound when it is highlighted and clicked? 0 Answers

How do I access a variable from a different scene using C#? 3 Answers

how to create a scene in c# WITH a specific gameobject? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges