Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Call function from a script not attached to object?

Discussion in 'Scripting' started by jsipich, Feb 3, 2012.

  1. jsipich

    jsipich

    Joined:
    Feb 3, 2012
    Posts:
    87
    Hey all! This is my first post here so I'll give a quick background:

    I know a bit of C++ from learning it the past few months and am getting into learning Unity and C# (I figger it's a better fit since I'm reasonably good with C++).

    In programming C++ - I would often create a mess of functions particular to a single aspect of a game for tidyness... ex: keep my inventory management functions in one script, combat functions in another, public variables or random functions which I can call as needed from anywhere, so on and so forth.

    I did this to basically keep things nice and tidy in my code so I wasn't digging through single scripts thousands of lines to find what I needed.

    Can I do the same with Unity? Have a separate script which is NOT attached to a game object and call functions or public variables from within?

    Example: Damage calculator function located in combat_scripts.cs (or whatever I want to name it).

    Perhaps I want to use this function in multiple scenarios and is not indicative to a single object either player, enemy or random world damage... basically... wherever I want to use this math function - it is available.

    Do I have to attach the "combat_script.cs" to an object in order to reference functions from within the script? Or can the script merely be part of my project assets and I can call functions or variables from within that script whenever I want?

    I'm assuming removing the Monobehavior since it won't be attached to an object?

    I've only been "mucking" around with unity and C# for about 3 days and having a blast. Learning Unity3D through the book "Unity 3.x Game Development Essentials" (gets my high five!) and C# through various books here.

    I always like to "stray" a bit from the books - as I was learning C++ I would often stop at a particular chapter and mess around building stuff with what I've learned - testing - researching and trying different approaches - makes the learning process more fun for me.

    I would like to do the same with Unity3D - as I write scripts particular to this book I'd like to "experiment" with different approaches - try things the long way - try things the short way - look for ways to optimize code, etc.

    I just flat-out love toying around and wasn't sure how Unity3D or C# would work with respect to calling functions from other scripts not attached to objects. I know you can call functions or get variables or whatever you need from scripts attached to objects and that is cool.

    But I was just curious as I like having global or public functions I can use scene after scene even without the same object being used in a different scene.

    Anyhow, I appreciate any thoughts, suggestions, comments and high fives to this newcomer.

    Sorry for the long post!

    Thanks again in advance! :)

    - Jack
     
  2. Diviner

    Diviner

    Joined:
    May 8, 2010
    Posts:
    677
    You can and you should.

    Remove the Monobehaviour inheritance and don't attach it anywhere. Declare all helper methods in your custom class as Static and you'll be able to access them by referencing the name of the class. Alternatively, you can create an object of said class and call these methods through the object reference. Your decision.
     
  3. jsipich

    jsipich

    Joined:
    Feb 3, 2012
    Posts:
    87
    Awesome! Thank you!

    Just a wild guess from my limited time with C# - scriptA to call a function in scriptB:

    Code (csharp):
    1. //random function in scriptA
    2. void randomFunction () {
    3.     scriptB.function();
    4. }
    If so:

     
  4. Diviner

    Diviner

    Joined:
    May 8, 2010
    Posts:
    677
    ScriptB:

    Code (csharp):
    1.  
    2. public class HelperFunctions {
    3.  
    4.        static int SomeFunction( ) {
    5.              
    6.                //Some Code
    7.                return 5;
    8.  
    9.        }
    10.  
    11. }
    12.  
    ScriptA :

    Code (csharp):
    1.  
    2.  
    3. public class ScriptA : MonoBehaviour {
    4.  
    5.        void Start ( ) {
    6.            
    7.              int y = HelperFunctions.SomeFunction();
    8.  
    9.        }
    10. }
    11.  
     
    plantboy808 likes this.
  5. jsipich

    jsipich

    Joined:
    Feb 3, 2012
    Posts:
    87
    Perfect! Thanks again, Diviner.

    Now it's time to play around with functions XD
     
  6. ZoomDomain

    ZoomDomain

    Joined:
    Aug 30, 2012
    Posts:
    150
    Could someone give the solution for javascript please i can code ok but it says Object reference not set to an instance of an object when i call the function from a js.
     
  7. sourav13

    sourav13

    Joined:
    Oct 9, 2013
    Posts:
    3
    hi all
    there is a similiar issue with me and i also want an answer and my problem is same as ZoomDomain is having.
     
  8. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229