Search Unity

how to make a function public in unityscript(js)

Discussion in 'Scripting' started by TheRaider, Jun 7, 2011.

  1. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    Just as it stands.

    I figured I should be using functions to communicate with the script rather than using global variables to make it more robust.

    I just want to be able to call the function from another script in the same way as global variables.
     
  2. Vinit

    Vinit

    Joined:
    May 21, 2011
    Posts:
    21
    In order to make function public, first write a function in a separate script. Then, define a variable say target1 of that function script type in each script wherever you want to call that function. And then drag that function script into target1 column in the inspector window. Finally use that function using following statement :

    target1.functionname();

    Its as easy as it can be. Hope this will help...:)
     
  3. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    There are 2 main methods for calling script function from other scripts. You can use GetComponent() to create links to other scripts, which would allow you to access their function. This is usually done early on in the script (in OnAwake()).

    For example, Script 1 uses GetComponent(Script2) to create a link to Script 2, and can then use Script 2's functions.

    This of course requires you to search and link each script when/where you need it.

    EDIT: This is basically an automated version of what Vinit suggested above

    If you've already considered this and want a different way, you could alternatively use static functions. Here's a code example:

    Code (csharp):
    1. static function MyFunction (string : String) {
    2.     Debug.Log(string);
    3. }
    If you where to save this in a file called TestScript.js, you could then call MyFunction() from any other script in your scene without having to do any component linking:
    Code (csharp):
    1. TestScript.MyFunction("Hello World");
    There are pros and cons to both methods really.

    You could of course also just declare your global variable as static and access them in a similar way (in fact if the functions are purely for Get/Set functions for global data, it would need to be declare static as well).
     
    Last edited: Jun 7, 2011
  4. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    you mean like var myScriptName : Script; except unity doesn't recognise that.
     
  5. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    it should be var VarName : ScriptName :)
     
  6. pezz

    pezz

    Joined:
    Apr 29, 2011
    Posts:
    606
    Yes that works perfectly in unity. In start or awake function you need to find object of type.
     
  7. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    Nomad when I use your method I get the erros "An instance of type 'scriptnamehere' is requied to access non static member 'varaiblehere'"

    I guess this means functions can only access global variables using the second method.

    So I just need to know how to declare for the first method, since I would like to know how to do it, although I think I will use the second one for my particular situation.
     
  8. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    Sorry i forgot to say that the script containing the static functions needs to be on an object in the scene somewhere, but just once
     
  9. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    it says does not denote valid type
     
  10. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    that wasn't what the error was. I think it is saying that if you have a static function it can't access variables in the script that aren't static.

    thank you for the help :) I got it working that way. I still want to figure the other way.
     
  11. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    If you want to manually link scripts together for 1st method, you need to declare like:
    Code (csharp):
    1. var VarName : ScriptName;
    Unity lets you treat a script's name as a type.

    If you'd like an example of automatically linking, I can write one this afternoon (little busy at the moment)
     
  12. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    ahhhhhhhhhh I get it the Script isn't the type is is the actual name of the script.

    I wouldn't mind seeing automatically linking if you aren't talking about 1 of these 2 basic methods since I now have them both working :)
     
  13. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    It's an expansion of the 1st method, but instead of having to link the scripts manually in the inspector you get you scripts to find the links themselves when the scene starts.
     
  14. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    That sounds neat I would love to see it when you get time. I would appreciate it.

    On your game on shockwave do those 3 slots at the start become saves for each different person? If so how did you handle that?
     
  15. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    We used PlayerPrefs for saving and loading data.

    More specifically, for Aftermath, we basically had an array of custom class that stored all the player specific data. Selecting one of the profiles sets a PlayerIndex so we know which data we are using for level progression checks and achievments, and this index is also used to as part of the saving function to make sure we save the data to the right slot. For example: "Player" + PlayerIndex + "Name" would give us "Player1Name" for the Key part of PlayerPrefs saves.

    When the game starts it loads all 3 sets of player data to calculate the % done for the profile page.
     
    Last edited: Jun 7, 2011
  16. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    but don't you only have 1meg with the player prefs? or are you just going to worry about if you end up getting too many players?
     
  17. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    PlayerPrefs data is stored on the local machine that's running the game, so everyone that playes it gets thier own 3 profile slots to play with. If they changed to another machine, they'd have a different set of slots again. It is still limited to 1mb, but that's actually quite a lot bigger than it seems when it comes to the amounts of data you typically need to save.