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 Oorlfe · Jan 07, 2020 at 11:02 PM · levelsreuse

Strategy for reusing scripts on multiple levels

Hi,

I have many smaller scripts that connect to a main one that builds my level and runs it. The main script accesses the smaller ones and many of the smaller ones access the main script.

I make more levels. Each of them has its own main script because the builds and the gameplay of each level change and there will be many levels. All of the smaller scripts remain the same, but I can't figure out an efficient way to tell them to access a different main script depending on the level. As far as I know or can find I can only access a script if I identify it specifically by name when assigning it to a variable. Each level has its own main script with its own name. Is there a better way to set up my scripts for this?

Thanks for any help.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by unity_ek98vnTRplGj8Q · Jan 07, 2020 at 11:34 PM

This may not be a viable solution based on exactly how your project is set up, but this sounds like it may be a job for Inheritance/Polymorphism, one of the reasons object oriented programming is great. Make a base class (maybe called something like MainLevelScript) and then have each of your main scripts inherit from this script. Then your objects could interact with this script regardless of which type it is.

 public abstract class MainLevelScript : Monobehavior{
 
     public abstract string name;
 
     public static MainLevelScript currentLevel;
 }
 
 public class LevelOneScript : MainLevelScript{
     public override string name = "Level One";
 
     void Start(){
         MainLevelScript.currentLevel = this;
     }
 }
 
 public class LevelTwoScript : MainLevelScript{
     public override string name = "Level Two";
 
     void Start(){
         MainLevelScript.currentLevel = this;
     }
 
     public void DoSpecialLevelTwoStuff(){
         Debug.Log("I am in level 2");
     }
 }

Now here's an example script of how you could interact with your main scripts

 public class SmallScript : Monobehavior{
 
     void Start(){
         MainLevelScript mainScript = MainLevelScript.currentLevel;
 
         //I don't care what type the script is, I just want to know the level name
         Debug.Log(mainScript.name);
 
         //Do stuff only for some levels
         LevelTwoScript l2script = (LevelTwoScript)mainScript;
         if(l2script!=null){
             l2script.DoSpecialLevelTwoStuff();
         }
     }
 }

Comment
Add comment · Show 1 · 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 Oorlfe · Jan 09, 2020 at 12:04 AM 0
Share

Great! Thank you

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

117 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

Related Questions

Reusing same 2D Environment in different levels 0 Answers

New scene for minigame or not? 1 Answer

Display Audio Analysis and levels with objects 0 Answers

when i click the GUI button it doesnt load the level 1 Answer

Unity Serializer Wont Load Saves 1 Answer


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