Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Calling function outside namespace

Discussion in 'Scripting' started by EnderManWiggins, May 20, 2020.

  1. EnderManWiggins

    EnderManWiggins

    Joined:
    May 22, 2019
    Posts:
    29
    So I got a network system from the asset store and when it receives a message a function runs. I want to be able to call a different script that is one the same gameobject but is out side of the namespace that the network has.
    (Putting the other script in a namespace is not a option)

    thanks EnderManWiggins.
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    You can access stuff from any namespace by typing the full namespace name.

    For example, the List<T> class is in the namespace System.Collections.Generic. Most people put "using System.Collections.Generic" at the top of their file so that they can create lists just by typing something like

    List<int> myListOfInts = new List<int>();


    But if you don't want to "use" the whole namespace, or if you have a naming conflict with something called "list" in a different namespace, you can instead type

    System.Collections.Generic.List<int> myListOfInts = new System.Collections.Generic.List<int>();


    A common example of a conflict in Unity games is "Random", because there is a UnityEngine.Random class, but there is also a System.Random class. If you put both "using UnityEngine" and "using System" at the top of your file, then in order to access either one you will need to type the full name to disambiguate them.
     
    tomwilswood likes this.
  3. EnderManWiggins

    EnderManWiggins

    Joined:
    May 22, 2019
    Posts:
    29
    I know that but how do I call a function in a different script that is not in a namespace, from a namespace?
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    You...just type its name?

    Technically, it's the same answer as before: you write the thing's full name, which in this case happens to be the same as its local name because it's in the global namespace.

    Code (CSharp):
    1. // Not in a namespace
    2. public static class Foo {
    3.     public static void SomeFunction() {}
    4. }
    5.  
    6. namespace SomeNamespace {
    7.     public class Bar : MonoBehaviour {
    8.         public void Start() {
    9.             Foo.SomeFunction();
    10.         }
    11.     }
    12. }
     
  5. EnderManWiggins

    EnderManWiggins

    Joined:
    May 22, 2019
    Posts:
    29
    I figured out that much before but I cannot have a static monobehavior.
     
  6. EnderManWiggins

    EnderManWiggins

    Joined:
    May 22, 2019
    Posts:
    29
    I figure out how I just need to move the class into the same script
     
  7. EnderManWiggins

    EnderManWiggins

    Joined:
    May 22, 2019
    Posts:
    29
    But then it cannot call out to the other functions in different scripts
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    You don't need to move anything. You just need to get an instance of that object to call the method on. Make sure there is an instance of it in the scene, then you can do this:

    Code (CSharp):
    1. MyOtherScript theInstance = FindObjectOfType<MyOtherScript>();
    2.  
    3. theInstance.TheMethod();
    FindObjectOfType is not a great idea to use all the time, as it is slow, but for quickly getting things working right now it's fine.
     
  9. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,385
    Yeah, this doesn't appear to be a namespace issue.

    It's a "have a reference to the object" issue. In which case... which object?

    @PraetorBlue demonstrated a way to get a reference to a object, but it'll just be the first on found in the scene. Which one do you expect to call it on? Is it attached to the same GameObject? You could do 'GetComponent' on the local gameobject. Or you could have a field/variable as a member of the first script, and you drag a ref to MyOtherScript onto it, and access it that way.

    There's a multitude of ways to get references to objects... the way you choose has more to do with... well... which one do you want? There could be 1000 MyOtherScripts out there!
     
  10. EnderManWiggins

    EnderManWiggins

    Joined:
    May 22, 2019
    Posts:
    29
    hmm let my go and check
     
  11. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    Because you phrased the problem as needing to call into another namespace, I assumed that you already knew how to solve the problem if the other class were in the same namespace. But as lordofduct says, it now sounds like your problem has nothing to do with namespaces, it's that you need a reference to the object that you want to access.

    If that's wrong and you know exactly how to do what you want if only the things were in the same namespace, then please post what you would do in that case and we can help you adapt it to cope with a different namespace.
     
  12. EnderManWiggins

    EnderManWiggins

    Joined:
    May 22, 2019
    Posts:
    29
    thanks. I figured it out by removing the namespace.
     
  13. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,385
    I am so confused as to what the problem actually was.
     
    orionsyndrome and PraetorBlue like this.
  14. EnderManWiggins

    EnderManWiggins

    Joined:
    May 22, 2019
    Posts:
    29
    Yeah I am not good at describing problems through text.
     
  15. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,385
  16. stvnkndsn

    stvnkndsn

    Joined:
    Aug 9, 2020
    Posts:
    1
    Sorry for the necro post, but I think I know what this man's problem was, because I'm experiencing the same issue. I just downloaded the AstarPathfinding asset, which is an easy setup for adding 2D pathfinding. In the namespace Pathfinder, there's a function called public virtual void OnTargetReached () which runs when the character arrives at its destination. However, I can't reference my original scripts from that function and I'm not sure why.
     
    mickeyband likes this.
  17. KateKomar

    KateKomar

    Joined:
    Dec 9, 2020
    Posts:
    7
    Hi!
    I have the same problem with the AstarPathfinding asset - can't call any of my classes.
    Is anyone can give a hint - why is this happen?
     
  18. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,573
    A lot of packages are shipped with assembly definition files. So the content of the folder is compiled into a seperate assembly. That means scripts outside that package can reference classes and types inside that package, but not the other way round. Assemblies can not reference each other. Circular references are not possible. In many cases you are not supposed to edit any of the scripts in such packages. You either use events or delegates that those classes may provide, or maybe you can create a subclass in your normal scripts folder that is derived from the class in the package.

    I haven't use the package you're talking about so this is a pure assumption. However it's a common issue and has nothing to do with namespaces.

    I'm not sure which "AstarPathfinding" asset you're talking about. However the only one I know of is the Aron Granberg A* pathfinding project. By looking at the changelog we can see that they added assembly definition files in 2018 to the project. If that's the project you're using, I suggest to have a look at their specific tutorials and documentation on how to use the package.
     
  19. PuzzleBuilder

    PuzzleBuilder

    Joined:
    Jan 1, 2017
    Posts:
    12
    I was running into this issue of not having access to my scripts/classes from an asset script and it was caused by assembly definitions. In my case it was a different asset, but this may still apply. Many packages contain assembly definitions in order to speed up compilation. You can find them in the project by looking for the puzzle icon.

    One solution is to create your own assembly definition for a particular script or set of scripts and add it to the asset's assembly definition references (see link below for how to do this). However, the scripts in your assembly couldn't reference any of the asset's scripts, because it would create a circular dependency, like Bunny83 mentioned, so this may not be what you're looking for.

    https://docs.unity3d.com/Manual/ScriptCompilationAssemblyDefinitionFiles.html

    You'll likely want to create an event in the asset script, subscribe to it in your script, and pass any relevant data from the asset script to your script when the event is called. This way you'll have access to all of your references and the data from the asset script, and your code will be better decoupled from the asset code. Quick overview of all you need to know about events and delegates in Unity:

    https://gamedevbeginner.com/events-and-delegates-in-unity/
     
    Last edited: Mar 9, 2022
  20. adamstepinski

    adamstepinski

    Joined:
    Aug 7, 2015
    Posts:
    56
    For me it was assembly definition file. Look for this icon in folder with scripts from which you can't reference global namespace (top icon is assembly definition file, bottom 3 are script files.
    upload_2023-3-21_17-39-45.png
    Basically everything in folder with assembly definition file is cut from your project, you can reference these scripts from your project but scripts in that folder can't reference your project.

    If you remove Assembly Definition File you should be able to access your project but the compile time will be longer.
     
    lsheslop likes this.