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 /
avatar image
9
Question by Disaster · Apr 30, 2010 at 04:24 PM · scriptingbasics

Getting a script to run without attaching it to a GameObject

I was wondering if there was a way to run a Manager / Singleton class when my game starts without attaching it to any gameObject. I need this class to hold references to my current camera and other such objects which I'd like to access from a variety of different scenarios.

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

4 Replies

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

Answer by duck · Apr 30, 2010 at 04:42 PM

You need at least one script attached to a gameobject somwhere in your scene.

However you can use that script's Start() function as a hook from which to instantiate any number of your own classes which themselves aren't attached to a GameObject.

If you're going to create new instances like this, make sure that they don't inherit from MonoBehaviour, since the way that MonoBehaviour works makes some assumptions that it is attached to a GameObject.

So the rule for creating instances of scripts at runtime are:

  • For MonoBehaviours, always use:
    gameObject.AddComponent(MyScript)

  • For non-MonoBehaviours, always use:
    new MyScript();

In summary, you can have scripts running which aren't attached to GameObjects, but you can't get scripts running in the first place without at least one attached to a GameObject, to "kick things off".

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 Disaster · Apr 30, 2010 at 04:46 PM 0
Share

So, in theory, I could just add a empty gameObject and attach my script to that?

avatar image yosh · Apr 30, 2010 at 10:35 PM 1
Share

Yeah, thats right. Im creating always a "Controller"-script and a attach that to an empty gameobject. Theres my whole functionality for key presses, loading scenes, objects etc. Here you can access other gameobjects/components, too..

avatar image
17

Answer by JacobK · Sep 27, 2017 at 01:25 PM

This is possible since Unity 5 or so, using [RuntimeInitializeOnLoadMethod]

See: https://docs.unity3d.com/ScriptReference/RuntimeInitializeOnLoadMethodAttribute.html

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 Harinezumi · Jun 18, 2018 at 03:44 PM 1
Share

I'm pretty experienced with Unity, yet this is the first time I've heard of this (see$$anonymous$$gly) extremely useful attribute! If this works as expected, it is amazing!

avatar image
3

Answer by yeahdixon · May 21, 2014 at 10:07 PM

IN addition you can have a singleton class that is not a monobehavior, that has links to game object in your scene. You can Call : AppState.Instance().goGame=this.gameObject;

and can be called from within empty game object script.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class AppState  {
 
     public static AppState _instance = null;
 
 
     //holder for reference
 ;
     public GameObject goGame;
 
 
     public AppState() {
 
         Debug.Log("constructor ");
 
     
 
     }
 
 
 
     /* singleton */
     public static AppState Instance() { 
         
         if (_instance==null)
         {
             _instance=new AppState() ;
 
         }
         return _instance;
     }
 
 
 }
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 UnitedVector · Aug 26, 2018 at 03:21 AM

Make your class extend UnityEngine.ScriptableObject, then use the ScriptableObject.Awake() function to do what you want

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 LukeNukem44 · Jan 30, 2019 at 03:13 PM 0
Share

Just been testing that. -- It's doesn't work on it's own if it's just a script in your assets.

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

10 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Is all scripting GameObject based? 1 Answer

Click gameObject and then pop up window 1 Answer

What is a Script when it's attatched to a GameObject? An Object? A Class? A Reference? 3 Answers

!!!URGENT need help with gravity UGENT!!! 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