Unity Community


Results 1 to 11 of 11

  1. Posts
    561

    Global Scripts.... how and where?

    I want to have some globally accessed scripts that are not associated with any objects.

    Do i need to create some empty objects to use as a base for these global scripts/variables and attach the scripts to it?


  2. Location
    Greenwood, AR
    Posts
    188
    Quote: It doesn't matter if we win or lose, it's how we make the game!

    Mobile Releases:
    Ultra Line Software

    Tutorials:
    C# Audio Manager
    C# Pathfinding + txt + iTween
    C# 2D Snake Clone
    Scene Transitions

    Contact:
    Larry Pendleton


  3. Location
    The sunken city of R'lyeh
    Posts
    2,061

    Re: Global Scripts.... how and where?

    Quote Originally Posted by Ramen Sama
    I want to have some globally accessed scripts that are not associated with any objects.

    Do i need to create some empty objects to use as a base for these global scripts/variables and attach the scripts to it?
    What you're looking for is a singleton class. There's numerous ways to implement them and no you don't always need to attach it to a game object. it's an oft discussed topic.

    http://www.unifycommunity.com/wiki/i...itle=Singleton

    You can start there then search around the forums for other examples, find one most appropriate for your situation.


  4. Location
    Denmark
    Posts
    150
    I'd go for static variables/functions in a seperate class/file


  5. Location
    The sunken city of R'lyeh
    Posts
    2,061
    Quote Originally Posted by Zigs
    I'd go for static variables/functions in a seperate class/file
    Doing that is akin to asking someone to walk through a minefield with magnetized iron boots on.


  6. Posts
    561
    Quote Originally Posted by Quietus

    Doing that is akin to asking someone to walk through a minefield with magnetized iron boots on.


    I created a script and added static function and was able to call it from other scripts. I guess this is basically what i want.

    How is doing it that way a bad idea? That singleton class seems redundant when i can just do it with static variables.

    Care to explain more? I come from using Torque. And with torque you simply called a function or variable by it's name. Basically all functions/variables in scripts were accessible to any other function.


  7. Location
    Denmark
    Posts
    150
    Quote Originally Posted by Quietus
    Doing that is akin to asking someone to walk through a minefield with magnetized iron boots on.
    I don't see the big problem. If you want something to be global, why not just make it grobal?
    On the other hand, It's rarely necessary to have such global functions and variables. This is exactly why object orintation is supirior to procedural coding, so don't overdo it.
    Please explain why it's necessary to drag Singletons into it?

    - Chris


  8. Location
    ěresundsregionen
    Posts
    465
    I would also like to know whats wrong with a simple static class.

    We use that all over vs. the wiki-singleton pattern, and it has yet to bite us in any way.
    ElectroCute * Smack Boxing * TouchWars * Monster Ball * Smack Hockey * Tactical Soldier
    Unity SmartFoxServer API * Unity Asset Server Commit Mailer
    Follow on twitter: http://twitter.com/thomas_h_lund


  9. Location
    The sunken city of R'lyeh
    Posts
    2,061
    Quote Originally Posted by Thomas Lund
    I would also like to know whats wrong with a simple static class.

    We use that all over vs. the wiki-singleton pattern, and it has yet to bite us in any way.
    There's nothing wrong with using a static class if say you're looking for a game/score manager of some type.

    Static variables and functions come with some pitfalls, one just needs to be aware of them. For example, it seems in every one of the thousands of the 'How to access variables in another script' threads, someone will end up suggesting 'just make it static!' That poor user then wonders why his score isn't reseting between scenes, why he now has variables defined two scenes before the script is even loaded, etc.


  10. Location
    ěresundsregionen
    Posts
    465
    Ahhh yes - but that goes for singletons too and other patterns - use when appropriate and not before having read the "manual".

    Just because we are making games doesnt mean that proper computer science knowledge is required

    /Thomas
    ElectroCute * Smack Boxing * TouchWars * Monster Ball * Smack Hockey * Tactical Soldier
    Unity SmartFoxServer API * Unity Asset Server Commit Mailer
    Follow on twitter: http://twitter.com/thomas_h_lund


  11. Posts
    561
    One thing i did discover was that you don't have to have a script attached to an object to call functions from that script. For example, you can have a script called TestScript.js in your project folder somewhere.

    In a script attached to an object you can call a function from that script file even though it's not in the scene... using:

    Code:  
    1. TestScript.functionname();


    this is basically what i was wanting. Glad you don't have to have every script attached to an object to use the scripts. I'm sure nearly everyone knew this, but i'm new

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •