- Home /
Where can I find Unity Script Tutorials that teach how to make the scripts come together?
I have been looking around for a long time at various tutorials to teach me Unity Script, and have had great deal of luck every time I am looking for a specific solution to a specific problem Im having. I would still call myself a beginner with scripting and I've been having a lot of problems finding help from online tutorials that gets me past the initial steps of learning to script. I'm having trouble thinking of a good way to explain my problems, but here goes:
The first book I got a long time ago when I was learning ActionScript for Flash said that using OOP was like building all the pieces that were needed for an airplane, and then putting them together. To make the landing gear, you need to write the wheel, the wheel socket, the brakes and the part that connects them all to the plane. I understand how to script all the parts fairly well, but I am having trouble figuring out how to fit them together and make them interact effectively with one another.
I know a need a main systems script that I assume will not be attached to any GameObject, but I do not know how to call from it when using scripts attached to game objects in the scene. I am trying to create an old school JRPG and I am thinking that I will need to create at least 4 game modes at the top level, a normal mode where I wander around and interact with objects and NPCs, and event mode where everything is paused but animations of specific characters and items I am interacting with, a menu mode that pulls up the game menu with all the stats and items and equipment and whatnot, and a battle mode where fighting takes place.
My question for this instance is where are these game mode scripts located, how do I access them with other scripts and what types of scripts should I be using? I am not very experienced in creating classes and utilizing them with other scripts, should I create these scripts as classes? I'm thinking my questions are more related to creating the basic systems scripts, and as I said, I am having a lot of trouble finding tutorials that go over the way to build a network of scripts that work together behind the scenes. If anyone could point me in the right direction to a tutorial, or give me some simple explanation (Im doubting this exists) that will set me on the right path it would be very helpful.
I can give one more example of something Im having trouble figuring out how to put together, an inventory that lets you pick up items. I know I could create a class that would be for items that would have variables for the name, an image and description of what it does, then make sub classes that extend with functions (healing item, equipment, key item etc.) that tell the items to heal or add stats or what not, but Im not sure if I can then just push those scripts into an array that tells the number of that item you have for the inventory or if I have to instantiate the object for each time its collected. Also how to call that item in another script, for example would it be potion.HealingItem.Item(use) or leathervest.Equipment.Item(equip), or whatever.
I am also thinking having separate scripts that outline the functions an item could do that would call on variables within the items scripts should be the way to go. For example make a script for healing that would add x amount of hp to a target, an antidote that would turn a poison effect from true to false, or do nothing if it was false already. That way I could call these scripts and use them for abilities as well. How would I call on these functions from different scripts in the item script? Would I have to make all these functions in a class script? Or do I have to write these into the item script for each item every time?
A tutorial on how to make all that work would be really helpful as well.
I understand how to attach a script to a GameObject, and how to make GameObjects interact with each other, but the problem Im having is attaching scripts to other scripts and making them interact with each other.
I know the questions are vague and likely have multiple answers, so I guess I need vague answers on the bigger picture, making the pieces of Unity Script work together, not just how to make specific scripts do specific things. Again, any help or nudges in the right direction would be greatly appreciated.
edit: Forgot to mention I am using Javascript to code.
here are some good things to know for script interaction (this is assu$$anonymous$$g you have a basic understanding of common program$$anonymous$$g concepts, and explains some unity specific stuff)
keep ur scripts in the same language, c# is best
the GetComponent() function allows you to reference components(scripts) on the instance of the gameobject u call it from. subsequently you can use the static find functions (GameObject.Find()/FindGameObjectWithTag()/ect.) and use the GetComponent()/GetComponents() from the gameobjects the find functions retun.
using public staic variables will allow you to alter the variables withoyut creating an instance of the class or referencing via getcomponent, keep in $$anonymous$$d tho, static variables cannot be set in the inspector
learn about events, you can publish an event from a class and have other classes subscribe functions to the delegate of said event and then those functions will be called when that event happens
just to avoid confusion scripts/components/classes are interchangeable vocabulary in this case
Thank you both, the comments were very helpful. I found some tutorials on events and feel like things are starting to come together.
I think I'm still a little confused on using a class vs. using a normal script. If I were to create a class and wanted to utilize it in a script (not specified as its own class, I am under the impression that there is a difference) attached to a GameObject would I have to use GetComponent() to use it? Are classes (and any script for that matter) GameObjects and called in the same way?
Also, I am using Javascript because I have a bit of experience with it from flash that has carried over and makes it easier for me. Is C# that much better that I should start learning to script with it ins$$anonymous$$d?
i would highly suggest c#, u dont "need" it and can get by without it, but it allows you to do more plus it runs faster at its core. once you are familiar with all the syntax in js, then move on to c# and it will be an easy transition. as for the clas thing let me explain
every scrip is a class, u can have more than 1 class per script, but one has to have the same name as the script (in js u dont worry about the same name part, and unity automatically handles the na$$anonymous$$g when u create the c# script), furthermore that class must inherit from monobehaviour. this gives us our event functions such as On$$anonymous$$ouseEnter(), OnCollisionStay, ect. the component is the entire script with all the classes thats in it, however in 95% of all your scripts you will only have your main class, so they are all the same thing in those cases. if you use the GetCOmponent() function it will return the main class only, however the other classes are still compiled and you can create new instances and refer to any public instance located in the main class from GetComponent() or any other reference to a class you may have
I don't really suggest c# ! Unless you're building a proper game engine inside unity,and unless you're quite confident with mathematics and learning complex Scriptures,.js is better annotated and simpler, and after a year of it you can easily move on to c. you could very easily make all kinds of amazing games with unityscript, I did the worm game to learn unity, and then I set myself tasks like making cubes in lines of different colours and sizes etc, and then making them move and turn, and then your on a roll. I would just try putting things are different colours and sizes and positions on the start function with Instantiate. Learn Instantiate, rotation, localScale, and you've already make fun patterns
@ZoomDomain lol you must not know much about c# or other program$$anonymous$$g languages/ concepts in general, there is no integral part of any language that is any more mathematical than another. any language obviously requires arithmetic knowledge, and a 3d application (such as unity) requires some vector math knowledge; but this is knowledge required for js too. the major differences that could make c# harder are as follows: 1. no dynamic typing, solution = casting 2. non dynamic arrays, solution = Lists 3. if your already used to js syntax, it may be annoying getting used to new syntax 4. requires that u understand encapsulation and ineritance; however both of those are simple and are really a requirement to any kind of skillful program$$anonymous$$g in OOP
now reasons why i suggest c# 1. the lack of dynamic typing and arrays is intentional. it makes your code much much faster, and can be easily overcome with other features built into c# so that u use that processing power only when and where its needed for example
js: var exampleGameObject : GameObject = Instantiate(examplePrefab);
now this works fine in js but not in c#... "why?" u ask? well thats because Instantiate() returns a Object type and not a GameObject type like our variable type but js automatically converts it to GameObject for us, but this is all u gotta do for a c# script to fix it
c#: GameObject exampleGameObject = (GameObject)Intantiate(examplePrefab);
see? so all we had to do is change the variable declaration syntax to the standard syntax of c# (var exampleGameObject : GameObject -> GameObject exampleGameObject) and then the the cast "(GameObject)" which basically means that we want to convert the Object type from the Intantiate() function to a GameObject type which inherits from the Object type
Answer by Loius · Jan 04, 2013 at 09:09 PM
A'ight:
In Unityscript ("javascript"), you can create classes like this:
class X extends Y { /stuff/ }
If you don't have a "class X" line anywhere in the file, Unity assumes that the first line of the file is this:
class filename extends MonoBehaviour {
And of course it closes the bracket for you.
In C# you -must- include the class declaration line(s) yourself.
So - all your JS files are classes. You don't (and can't) have anything that's "just a script but not a class". Hopefully that helps clear some confusion away.
GetComponent returns a Component. MonoBehaviour is descended from Component. So if your class extends MonoBehaviour, you should use GetComponent (or another method of acquiring a specific Behaviour) to get it, and use AddComponent to add it to an object.
Say X extends MonoBehaviour ("is a component"), and you want the X off a specific GameObject Y: you'd use "myX = Y.GetComponent(X)", "myX = Y.GetComponent.()" (fastest), or "myX = Y.GetComponent("X")" (slowest).
If X does NOT have MonoBehaviour as an ancestor, then it's a non-component class, and you need to use 'new' to create it: "myX = new X()".
C# has a bit more punch to it at times. On the forum there are several extensive tests done on performance speed; for the most part, all three languages run at the same speed. They're compiled, after all. Off the top of my head, C# gives you much finer control over function arguments, like the ref keyword and delegates and such. I have a few C# classes in my mostly-JS project just to handle generics/refs and there's no issues. Use what you're comfortable with, and if you come up against a wall, consider using another language for that specific issue.
Last, "manager"-type classes are often static. For example, I usually have a "Game" class that handles stuff like level loading and game-over. The Game keeps track of important objects, but everything else runs independently of other objects, reacting to collisions or Game messages rather than trying to seek out specific objects and create long dependency chains outside their parent-child relationships.
Alright then ... that does clear a heck of a lot up for me.
When I was scripting with ActionScript you could add lines of script directly to the timeline, and you had to use classes to attach them to graphics, so with Unity Script there wouldn't be any difference between making a class and just making a snippet of script ... that changes my whole outlook on things.
I guess I need to look into static classes. So for my game mode class, it would be my manager class almost all classes besides classes that stored stats for characters and enemies would run from it, would that script/class need to be attached to a game object? Or could I just reference it from the other classes?
I was assu$$anonymous$$g that most scripts attached to GameObjects in the scene would reset when I changed over to different scenes and thought I would need a new kind of script to keep track of all the data that would need to remain constant as scenes changed (what I thought was the elusive class scripts).
Very helpful post, thank you so much.
I just found this and I don't think it was online before, it's a converter that changes unityscript to c# http://files.m2h.nl//js_to_c.php you can get a rough idea of the differences. What you will find with unityscript is that all the references online have unityscript examples, and around half or maybe more than that answers on the forum are for unityscript, especially the easy stuff.
if you are able to learn c# and you are good at maths some things and it's no worries them I would suggest that! If you're feeling like a wimp and need more online references to learn, UnityScript.
For now, I am a wimp. I think Im going to stick with UnityScript just because I want to start scripting and I feel like I am close figuring out how to make UnityScript function. Once I have a better handle on it I will likely learn C#.
That being said, I have tooled around with my new understanding and found linking scripts within a GameObject to be working correctly, but now Im having issues to get them linked up when they are not attached to the same GameObject.
Can static variables from a script be called no matter the scripts location? I guess I am wanting to call scripts from the project panel that are not located in the hierarchy, but I am starting to think this is not correct thinking.
Say I want a script that can be called from any other script on any GameObject, where do I put that script?
staic variables will have only one instance for all gameobjects within all scenes, meaning that if u change it from any place it also changes for all other scripts too
Answer by fafase · Jan 04, 2013 at 12:22 PM
here you can find a tutorial on script interaction and also other topics http://unitygems.com/script-interaction-tutorial-getcomponent-unityscript/