Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by RSud · Aug 17, 2010 at 02:23 PM · gamecontrolmaster

Newbie... Master script?

I am a newbie to Unity (but with game design experience) certainly asking simple questions....

I understand that scripts attached to gameobjects are called every loop of the game and the gameobject script is written to control that instance of a gameobjects actions within the game.

I am, for my first go at a game in Unity, planning a arcade style game where as the game moves along new enemies appear (with stronger enemies over time).

This needs a "master script" that is controlling the flow of the game. Among other things this master script would keep track of what enemies are being displayed now and as enemies are destroyed creating and placing the next new enemies.

How would this master script be implemented so it is also called every frame of the game?

I could call the master script from the player gameobject script and this would work.

But I think perhaps unity has some support for a master script that is not directly tied to any gameobject but is called every frame to provide control of the game.

Is this so? How is this implemented? If it is so I expect there is some documentation (which I am hapazardly reading so forgive me if its someplace obvious!).

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

5 Replies

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

Answer by SteveFSP · Aug 17, 2010 at 03:27 PM

Unity3D does not support running MonoBehavior scripts that are independent of gameobjects. (MonoBehavior scripts are the ones that get Update, FixedUpdate, and related function calls from the engine.)

Your initial thought was correct: You put your master control script onto a gameobject that will persist for the entire level. So the player gameobject is a decent place to put it. I tend to put my master scripts on a gameobject called "GameManager" so that their location is more obvious in the hierarchy.

[Updated to take into account Worlfram's comments]

Comment
Add comment · Show 2 · 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 Wolfram · Aug 17, 2010 at 03:50 PM 0
Share

That is not entirely correct, you could use the ScriptableObject class to create scripts that actually are independent. (which also means they won't have an Update() method, etc.)

avatar image Wolfram · Aug 17, 2010 at 03:51 PM 0
Share

...I forgot to add: "However, you rarely need them, if ever. I never had to use ScriptableObjects - simply adding "normal" scripts to empty GameObjects works quite nicely.

avatar image
2
Best Answer

Answer by Edy · Aug 18, 2010 at 12:33 PM

The best approach to a Master Script is an empty GameObject with the property DontDestroyOnLoad enabled. Attach your master script to this game object.

Note that loading/reloading a scene in Unity resets all GameObjects, their values and their scripts (except those with DontDestroyOnLoad enabled). So the Master Script is the proper place to control the global game flow and variables (i.e. levels, score, life...). The level-specific game flow (i.e. enemies on that level) could be controlled from an script in the player's gameobject, as it will be restarted on loading/reloading the level).

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
1
Best Answer

Answer by Wolfram · Aug 17, 2010 at 03:45 PM

Any script that's attached to a game object will have its Update() method called every frame.

You can simply attach your master script to an empty GameObject for that purpose. There are several methods of finding/referencing "real" GameObjects or other scripts from that script, to be able to modify these objects, or call functions in their scripts (using Tags (e.g., to find all enemy ships), or creating public var's to make them show up in the Inspector and then dragging the other GameOBject onto them, or the GameObject.Find() functions, or the SendMessage() methods, etc.)

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
2

Answer by Veli · Aug 17, 2010 at 03:44 PM

You can also use C# static classes to store some information or functions withouth them being in a gameobject.

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 Billu · Apr 01, 2016 at 04:05 AM

Usually you can attach the 'master script' on the camera. As long as the game continues, the camera are assumed on. Of course if your game switch cameras frequently this would not work

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

2 People are following this question.

avatar image avatar image

Related Questions

How to make camera position relative to a specific target. 1 Answer

How do i make an animation play on key press? 3 Answers

Scripting error #2! 2 Answers

get all scripts attached to gameobject 2 Answers

FPSMouseScript.js 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