- Home /
Noob q: how to access functions in script that is in asset folder/view?
Hi, I just snagged a graphing asset from the store and after import and placing it in the correctly named folders I can't figure out how to access a function in the main class.
I assumed I could create a gameobject to hold a new instance via Find then but visual studio isn't having any of it. So am I supposed to create a new gameobject to host the main class script?
I have googled the crap out of this but I can't seem to find a straight answer on the proper workflow.
Cheers,
Sorry I mean am I supposed to create a new gameobject in the scene for hosting the main class.
If my answer doesn't help: What exactly have you tried? What was Visual Studio complaining about? Generally, the documentation of an asset store product should tell you, what you are supposed to do, but maybe there's some other detail missing. You can post your code and steps you took so far if you need more detailed help. ;)
Answer by Xarbrough · Feb 25, 2018 at 12:28 AM
Is the class derived from MonoBehaviour? If yes, you can attach it as a component to a GameObject in the scene, yes. If not, it's intended to be used in code you write yourself. In this case, you would create your own script and add it to a GameObject to run. Within the code you may need to import a specific namespace via the "using" statement in C#, but then you can access the class. If it has static functions, you can just use them like Mathf.Abs(), but if the class is not a static utility class, you will have to create a new instance in C# code (this is different from creating components, but probably not the case anyway, and if it were there should be documentation about it).
That mostly makes sense, but I'm not clear that if I'm using a static class, then the script itself just hangs out in the asset folder (or subfolder) correct?
Yes, a static class can be accessed if the script is just placed in the project. Unity will automatically compile it. You can read up on C# compilation, if you're unsure what that means. C# is not a scripting language and so in Unity those script files are really just text files for you to tell the compiler what code to write for you. At runtime (with exceptions) you don't actually have any of the source code you write. Ins$$anonymous$$d, the text from the files is compiled into machine executable form over multiple sub-steps. This happens for every script in your project. Some of them may be derived from $$anonymous$$onoBehaviour in which case Unity knows how to create an instance of the class to work as a component on a GameObject and store that reference with a scene or prefab. You can also search for "instance vs static class" to find out more.