Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by davidwalmsley · Jan 04, 2013 at 04:31 AM · inventoryclassesinteractionsystems

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.

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sharpshot124 · Jan 04, 2013 at 12:13 PM 1
Share

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)

  1. keep ur scripts in the same language, c# is best

  2. 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.

  3. 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

  4. 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

  5. just to avoid confusion scripts/components/classes are interchangeable vocabulary in this case

avatar image davidwalmsley · Jan 04, 2013 at 05:25 PM 0
Share

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?

avatar image sharpshot124 · Jan 04, 2013 at 07:48 PM 0
Share

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

avatar image MountDoomTeam · Jan 04, 2013 at 08:28 PM 0
Share

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

avatar image sharpshot124 · Jan 04, 2013 at 10:30 PM 0
Share

@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

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image davidwalmsley · Jan 05, 2013 at 03:39 AM 0
Share

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.

avatar image MountDoomTeam · Jan 05, 2013 at 10:37 AM 0
Share

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.

avatar image davidwalmsley · Jan 06, 2013 at 03:18 AM 0
Share

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?

avatar image sharpshot124 · Jan 06, 2013 at 03:22 AM 0
Share

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

avatar image
1

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/

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Item database with override functions 0 Answers

AddItem() function simply won't execute? 1 Answer

I have objects that have the same set of variables, but different desired behaviors. How can I do this without repeating variable declarations across all behaviors? 1 Answer

Creating a unity insepctor dropdown menu with content 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges