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
0
Question by ExtremePowers · Nov 05, 2014 at 03:55 PM · classinstancecallvoid

Access class from all scripts

Is there a way I can call this code from all my scripts:

 public class Game : MonoBehaviour {
     private static Game _instance;
 
     public static Game instance {
         get {
             if (_instance == null)
                 _instance = GameObject.FindObjectOfType<Game> ();
             return _instance;
         }
     }
 
     public void Test() {
         Debug.Log("Test");
     }
 }

like this:

 Game.Test();

instead of having to do this:

 Game.instance.Test() 
 //Or
 Game g = Object.FindObjectOfType<Game>();
 if(g != null)
         g.Play();

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

Answer by Baste · Nov 05, 2014 at 04:10 PM

Yes there is. Make it static, and have the static version call a method on the instance

  public static void Test() {
      instance.TheActualTest()
  }

  void TheActualTest() {
      Debug.Log("Test");
  }

If you do this, you can also let the instance be private, and only access it through public static methods - that usually gives a lot cleaner code.

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 ExtremePowers · Nov 05, 2014 at 04:22 PM 0
Share

When trying to call it from another script with this:

 Game.Test();

I get an error "BCE0005: $$anonymous$$ identifier: 'Game'"

avatar image ExtremePowers · Nov 05, 2014 at 04:43 PM 0
Share

Nvm, I just placed the script in the Plugins folder :)

avatar image ExtremePowers · Nov 06, 2014 at 06:22 PM 0
Share

@Baste Is there a way to it like this:

 //In another script
 function Game.OnBeforeSpawn() {
    //Some code here
    Debug.Log("Test");
 }

class script:

 public class Game : $$anonymous$$onoBehaviour {
     public static void SpawnPlayer(Vector3 pos, GameObject PlayerObject, bool Respawn) {
         instance.OnBeforeSpawn ();
         GameObject p = Network.Instantiate(PlayerObject, pos, Quaternion.identity,0) as GameObject;
         p.name = PlayerPrefs.GetString("Username");
         p.tag = "Player";
             Debug.Log("Spawned Player");
     }
     
     void OnBeforeSpawn() {} //So declare this function from a script. So it would work something like Update(), like a built in function that can be modified.
 }

So if the script gets run like this from a third script:

 function Start() {
     Game.SpawnPlayer(Vector3.zero, PO, false);
 }

It would log "Test" to the console and then instantiate a the GameObject and log "Spawned Player" and if I where to have multiple of the first script it would be called no matter what gameobject and script it is in.

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

27 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

Related Questions

Class instance access to scene? 1 Answer

Coroutine design problem? 3 Answers

My void Start won't work 1 Answer

Call method from attached Gameobject 1 Answer

On Void Called 2 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