Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by cidra_ · Feb 06, 2018 at 06:01 PM · scripting problemscript.scenesingletonmanager

How can I handle scripts correctly? What's a good way to apply scripts to GameObjects?

Hi all. I'm new to Unity. In the past 6 days, I've been reading guides, browsing the manual and watching video-guides.

I'm making a simple 2d game in Unity for school which consists of 4 mini-games. On my first mini-game, the objective is to move a character on a 2d field with the accelerometer and to pick objects in a limited timespan. A very simple game. However, there's only one thing that puzzles me: where do I apply the scripts?

They are easy to write, that's true. But what's the best way to apply them to a gameobject?

If i make a script that destroys the collectible when the player touches it and instantiates a new one in a random position, i could place this script on the player GameObject, but then if i want to do "score++" each time player touches a collectible i should keep track of the score in the player script. If i wanted the timer to reset everytime player touches an object, i should keep track of time in the player script, and so also the GameOver() function should be put in the player script. I just attached the script that handles the entire mini-game on the player object.
What if i want the playerScript to call a function from another script (and from another object)? For example:

 void OnTriggerEnter2D(Collider2D trigger)
 {
     if (trigger.tag == "Enemy")
     {
         MiniGameManager.GameOver();
     }
 }

There could be hundreds of ways to bring the game to a "Game Over", but i can't always repeat the same code. A good way for me would be to make a mini-game manager that is visible to all the scritps in the scene. How can i put this in practice, without draggin and droppin the "mini-game manager" everywhere?

Thank you in advance for your availability.

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

Answer by Kishotta · Feb 06, 2018 at 07:09 PM

I'd highly recommend you use a Singleton for your game manager. As the name implies, you can only ever create a single instance of a singleton. You should be able to access it from anywhere in your code with something like:

  MiniGameManager.Instance.GameOver();
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 Matti-Jokipii · Feb 06, 2018 at 10:18 PM 1
Share

The OP should notice that not all scripts need to be $$anonymous$$onoBehaviours and instantitated on GameObjects. You can have your game manager singleton anywhere. Of course it may be convenient to make the game manager a game object, if it needs to interface with Unity engine i.e. respond to Start, Update, OnApplicationQuit and other such callbacks.

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

194 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

Related Questions

Singleton HELP 1 Answer

Teleporting problems 1 Answer

[Question] Unity C# Set a Player Class and refence issue 0 Answers

Reload Current Scene with Scene-manager 3 Answers

how can i save highest played level?,how can i save number of highest played level? 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