Search Unity

Accessing Other Functions Without Instance? Global vs Static?

Discussion in 'Scripting' started by login4donald, Dec 4, 2012.

  1. login4donald

    login4donald

    Joined:
    Jan 3, 2012
    Posts:
    462
    I know there are other forums that somewhat explain this but could anyone tell be is there a way to call a function in another script without an instance of it running? (It's an editor script with parameters) Also I heard making it 'static' or 'global' is a possiblity, I'd also like an explaination as well, but I have no I idea. I'd appreicated some help.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    you need an instance of an object to call a member of it (method, property, or field).

    static members can be accessed on the class, but static members themselves also can only access the static members of itself. It too would have to instantiate an instance of itself to access any non-static implementation with-in itself.
     
  3. fano_linux

    fano_linux

    Joined:
    Jan 1, 2012
    Posts:
    909
    it's called Singleton: Sample C#
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SingletonTest : MonoBehaviour
    6. {
    7.     public static SingletonTest Instance;
    8.  
    9.     void Awake()
    10.     {
    11.         Instance = this;
    12.     }
    13.  
    14.     public void Message(string message)
    15.     {
    16.         print("message: " + message);
    17.     }
    18. }
    19.  
    Another script class (it can be attached to another game object)
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class AnotherClass : MonoBehaviour
    6. {
    7.     void Update()
    8.     {
    9.         SingletonTest.Instance.Message("Hello there: " + Time.deltaTime);
    10.     }
    11. }
    12.  
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Singleton is yet another answer.

    But technically OP asked for "without an instance of it running", and a Singleton creates an Instance of it. Especially in the case of your MonoBehaviour, which means it had to of been added to a GameObject somewhere... so to access it, not only do you have to create an instance of it, but you have to create a GameObject for it.



    OP, what are you trying to create? A utility class or something? Why is it this function needs to be accessed with out an instance of it? Are you trying to create a class that holds several functions for utility purposes. This is called a 'static utility class', see here:

    http://msdn.microsoft.com/en-us/library/79b3xss3.aspx
     
    Last edited: Dec 4, 2012
  5. fano_linux

    fano_linux

    Joined:
    Jan 1, 2012
    Posts:
    909
    It would depend on the solution he is trying to find, but both ideas can be used.
    Knowing the way he can decide wich way is the best for his problem.

    :)
     
  6. login4donald

    login4donald

    Joined:
    Jan 3, 2012
    Posts:
    462
    Okay, I was trying to see if I could. There was a way you could do it but I don't remember. I use java btw but there was a way I saw:
    Code (csharp):
    1.  
    2. //the script in project, not in scene, Script A
    3. static function functionName (multiple arguements){
    4. //...do stuff
    5. }
    6.  
    7.  
    Code (csharp):
    1.  
    2. //and a script in scene, Script B
    3. function Start () {
    4. ScriptA.functionName(parameters);
    5. }
    6.  
    ADDED: Also how would I call a member of it?
     
    Last edited: Dec 4, 2012
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    that's a static function
     
  8. login4donald

    login4donald

    Joined:
    Jan 3, 2012
    Posts:
    462
    That's what I though but I get an error is the process. "Unknown identifer: 'Save'." Save is the name of the script(ScriptA in this scenario).
     
  9. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
  10. login4donald

    login4donald

    Joined:
    Jan 3, 2012
    Posts:
    462
    Oh okay, The editor files is compiled last making error is what I can presume so the script can't really find it.