Unity does not support JavaScript. It supports UnityScript, which is very similar to JavaScript, but has some important differences (like its handling of properties, support for type annotation, etc). I would not recommend learning JavaScript as a means of learning UnityScript, and in general I don’t recommend learning UnityScript anyway - it’s faster to get up and running with quickly, but it’s better to learn a more widely supported, robust, mature language in the long run.
As to the original question:
A method is just another term for a function - a sequence of statements (variable declarations, loops, sums, if-statements, etc) that are executed one after another.
A class is just a bundle of methods, along with some variables that get shared between them. So you can set the variables in one method (Start(), for example) and then use or change their values in another method (Update(), for example).
No need to study “deep” OOP, whatever that is. Not sure what “pure” C# is either.
I’m not sure there’s an easy way to describe OOP in just one post, but its not all that hard to learn basic OOP.
If you can make basic objects and have them “call” each others methods, you are doing pretty well.
I wouldn’t go much more complex than that until you can make classes and objects. It ought to cover Unity anyways which kind of locks you into its way of doing things. Some of the ways Unity works flies in the face of “Standard practice”, so I appreciate it can be quite confusing.
Start out with a really simple project. Make a cube and make it move in a straight line when the user presses a key. Gradually make it do more and more complex stuff.
Some of the caveats with Unity, as I mentioned above, are the way it locks you in. Two examples:
1 )You may read a tutorial that tells you, make a Gameobject class, a Snake class, make a Flower class. Make Snake and Flower inherit from Gameobject. Then when the Snake is near a flower, call snake.Eat(flower), etc. You can’t do that because in Unity your scripts are already deriving from MonoBehaviour, so you have to use the Unity inheritance model.
2) Variables exposed to the editor in Unity are public, which some people may regard as naughty, as other classes can freely tamper with their value.
After that if still confused, you should articulate what is confusing you, you might get an answer that will shine a light there.
Classes are blueprints for objects. When you instantiate a class, out pops an object.
You do something like Object x = new Object(); (Object is your class, now x is your object)
But in Unity if the object is MonoBehaviour, you have to use the Gameobject.Instantiate().
Yes.
Yes.
Yes.
You can do things like man.Sleep(bed).
In the Man class Update() method you could do, if man close to bed, man sleep bed.
You just need to put the bool CloseToBed() and void Sleep(Bed bed) methods in your man class.
I keep saying it; consider Python. You can learn to code fine in C# in just Unity, but you’re not understanding fundamentally what you’re doing. Python lets you see how it works and doesn’t bog you down in messy syntax. I highly, highly, recommend considering it. (I started coding in Unity three days ago [never used C# before either] and already have built my own First Person controller, Python gives you a very strong foundation.)
But why learning Class in C# is so complicated I am reading a guide PDF book and running C# 2010 Express at the same time.
So using Class in Unity is not the same as learning it in C#. I was reading about a text-based RPG game like Dragon Warrior from Webs.com has been shut down: Find out more
and the Class examples in this guide caused me a lot of headache out of not understanding a single letter. The Class was about the battle loop all I was doing is just reading and not understanding anything…
A class is, to put it in the simplest of terms, a new variable type. So for instance, the “list” class (or array if you prefer) is considered a variable type in most languages, I would declare it as:
myArray = [0,1]
Or some form like that (I use Python because it’s easy to read.) I could make my own class for a list.
class myList(self):
def __init__(self):
#code that happens on instantiation
def __str__(self):
#code when I call "print" on the myList type
def append(self):
#code for adding list elements
#outside of class
x = myList()
That code basically sums up very simply how a class works. If you go deep in to a language, you’ll find every variable type has a class associated with it, with methods. Adding integers is different from adding strings, but both call some form of the internal addition function.
If you understand the concepts, then all you need to learn is how syntactically to do it. I know the “how” and “why” of what classes do, so all I ever look up is how to type in syntax what I want to do.
Yes I don’t think that guide book you have is appropriate.
You were spot on with your Man and Bed class ideas, so you are obviously grasping the core concepts, you just have to get to the next level - How do make classes/objects do things.
The idea is to put behaviours in methods.
Create some methods and put these in your class that is attached to a Gameobject.
Don’t worry about animations, just yet. You’ll get there in time.
That’s why I suggested something really basic. Like a snake game, make a game where a snake moves on a flat surface, and is controlled by arrow keys.
Build up the complexity AFTER you have the basic movement done.
Fine, this is what I was waiting for?
So each of the previous actions needs a class right?
1- Make the snake eats blocks. So I should now add a new GameObject which is cube and attach a Class script to it call it Block Class for example?
Can I make then a behavior called Snake.eat.block?
No, actions are your methods (class specific functions), your classes are your overarching concepts. For instance, the snake itself is a class and the “eatBlocks” could be one of your methods. You want to make classes broad so you can inherit between them and really use the power an OOP language provides.
As you said, we’d say something like, snake is our class, the methods are: eatBlock, changeSpeed, and tailGrow
Something like that.