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 Squabbler · Oct 28, 2013 at 09:54 PM · c#instancesingleton

Accessing Singleton Instance Methods Directly

Is there a way to access a singleton method directly instead of going through it's instance?

 // Instead of doing this,
 GameManager.Instance.DealDamage(Target);
 
 // Is there a way to do this?
 GameManager.DealDamage(Target);


=====

GameManager.cs

 using UnityEngine;
 using System.Collections;
 
 public class GameManager: MonoBehaviour {
 
   // Singleton
   private static GameManager instance;
 
   public static GameManager Instance {
   
     get {
       return instance ?? (instance = GameObject.FindObjectOfType(typeof(GameManager)) as GameManager);
     }
   
   }
 
 
   public void DealDamage (GameObject Target) {
       // Deal damage to the passed target
   }
 
 
 }
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

2 Replies

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

Answer by Huacanacha · Oct 28, 2013 at 10:51 PM

If that's the behavior you want you should just use static methods, with static (class) variables if needed to store state.

 public static void DealDamage (GameObject Target)

If you don't need to hook into Unity's standard functions like Awake, Start, Update etc, then don't inherit from MonoBehavior.

If you do want to hook into the Unity functions you can use a singleton instance to update your static variables, without needing to provide public access to the instance.

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 Squabbler · Oct 29, 2013 at 03:55 AM 0
Share

I'm not sure if I will need the $$anonymous$$onoBehaviour inheritance yet or not. I just left it on there for now. Would it be better to remove it until something comes up that I need it?

avatar image Huacanacha · Oct 29, 2013 at 06:03 PM 0
Share

Great, I'm glad I could help. And thanks for accepting :)

I would remove the $$anonymous$$onoBehavior as it's unnecessary in the implementation you list below. I'll add more comments there.

avatar image
0

Answer by Squabbler · Oct 29, 2013 at 03:16 PM

For those wondering, this is how I ended up doing it.

=======

GameManager.cs -> Attached to GlobalScripts prefab (empty GameObject)

 using UnityEngine;
 using System.Collections;

 public static class GameManager {
 
     // Event for triggering damage to an object
     public delegate void OnDamageEvent(GameObject Go, int DamageAmount);
     public static event OnDamageEvent OnDamage;
     
     
     // Deals damage to the target
     public static void DealDamage (GameObject Target, int DamageDealt) {
         
         OnDamage(Target, DamageDealt);
         
     }
     
     
 }

=======

In the Enemy Controller

     // Attack the target
     protected override void Attack () {
         
         anim.Play("Attack");
         
         // Deal damage to target
         GameManager.DealDamage(Target, Damage);
         
     }

=======

In the possible target controllers

     void Awake () {
         GameManager.OnDamage += OnDamage;    
     }
 
 
     // Damage event method
     protected void OnDamage (GameObject Target, int DamageAmount) {
         
         // Check if we have this game object
         if (Target == gameObject) {
             
             TakeDamage(DamageAmount);
             
         }
         
     }
Comment
Add comment · Show 5 · 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 Huacanacha · Oct 29, 2013 at 06:28 PM 0
Share

In your current implementation you don't need to inherit from $$anonymous$$onoBehavior, and you also don't need the singleton methods as you never use the instance.

If you do want to leave in $$anonymous$$onoBehavior, the singleton technique I've seen used is to set the instance variable in the Awake method rather than via the get method of a Property, and attach the singleton class to a game object so Awake gets called. For organizational sanity I use an empty 'GameController' GameObject to attach all my game management scripts to (main GameController script, initialization etc).

I wasn't actually aware of C# Events as I'm fairly new to the language but they look useful... in fact I can simplify some of my own code now ;)

avatar image Squabbler · Oct 29, 2013 at 06:42 PM 0
Share

I tried to remove the $$anonymous$$onoBehaviour inheritance and the singleton code, but it wouldn't let me attach it to the GlobalScripts empty GameObject.

Is there another way I can access the Game$$anonymous$$anager script without attaching it to an empty GameObject?

avatar image Huacanacha · Oct 29, 2013 at 06:45 PM 0
Share

You shouldn't need to attach the script to a GameObject for it to work with static functions. The class is still loaded by Unity so it should just work.

avatar image Squabbler · Oct 29, 2013 at 06:49 PM 0
Share

Oh. Derp. I forgot to set the class to 'static' when removing the inheritance and instance.

public static class Game$$anonymous$$anager

lol, it works now. I never knew Unity would still read a static class without it actually being attached to a GameObject. This is a game changer for sure.

I also edited the code to reflect my changes.

avatar image Huacanacha · Oct 29, 2013 at 10:43 PM 0
Share

$$anonymous$$uch cleaner now :)

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

16 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

Related Questions

Singleton that can react to game events 2 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

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

Singleton code and Unify down. 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