Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

I want to learn c#... But

Discussion in 'Scripting' started by alzakielz999, Oct 6, 2019.

  1. alzakielz999


    Oct 2, 2019
    Im not sure where to begin and how/what to do. I've read a lot of ''You got to write code to learn'' and understand the basic. It sure make a whole lot of sense!

    But reading the basic and watching the examples doesn't seem to bring me anywhere. I feel like i understand what they are talking about, but im not sure what i could use it for. Im kinda overwhelmed.

    Hell if i managed to create a text-based labyrinth i'd find it awesome! But im reading reading and reading and i don't feel like i end up remembering much of the information and copying codes isn't really a good teacher i'd say?

    So i was wondering if there was anyone that could give me some ressources or give me an idea of how i should start or both would be awesome.

    I had started the course ''Create with code'', but while it's fun, i feel like i'd have to go and learn the basic after it anyway.

    Thanks you for your time!
  2. kdgalla


    Mar 15, 2013
    That looks like a good way to start. Once you've finished, just start by making a simple game. You can ask questions if you get stuck. You don't need to fully understand everything before you start. Some things only "click" with a lot of practice.
  3. Skaggsoft


    Aug 30, 2019
    Once you know what variables and methods are you should consider making a text-based labyrinth game. It's easier to stick to projects you're personally interested in.

    Whenever you get stuck look to google or the forums for help.
  4. JasonBricco


    Jul 15, 2013
    Don't rush it. It takes a lot of time to gain new skills, especially when they're not easy skills to acquire. Your brain will "structure itself", in some sense, toward programming as you do it more and then things will become more clear.

    Reading and watching won't be enough. You really do have to actually program. Even if you're writing exactly what the tutorial is showing, that's a good start. Then you can try to make small changes on your own to test your understanding.

    The best way to understand why you would use a certain thing is to come across the situation where it would be useful on your own rather than to take someone's word for it.
  5. MNNoxMortem


    Sep 11, 2016
    Start with something practical. Use unity if you feel like that is more fun to do so - altough I'd recommend starting without unity, because all it does is add one more layer.

    Start with a simple program which has a loop, waits for your input, allows you to enter commands, press enter and then does something until you close it with an 'exit' command. This is already a framework powerful enough to build something with it while not even leaving your IDE of choice (or the command line if you are one of this kind of persons).

    From there one you can build tiny text based games or make a calculator or a tool which does something useful for you (search text files and shows some lines if they fit your search criteria), and understand the basic structure and syntax of the language you are interested in to learn. If you extend this with writing and reading to a file you even have persistency. Which can be an incredible motivation factor for your first game, as it allows you to continue to play your save game with every iteration.

    Then you could start using unity to also have the ability to very easily and quickly add an UI, a window and on a click create a runnable application. However, be aware that whatever framework you buy into, means you will adopt of that framework and not C# in general. There is a lot of stuff one does in Unity, which he might do otherwise outside.

    Add some buttons, extend your functionality. Now extend your tiny game to also show it's state on screen. Congratulation you have made your first game.

    Extend this with some 2D functionality by simply moving some simple images on the screen. Now you have graphics.

    Then decide for a tiny little project, start over from scratch and builld it. No matter if 2D or 3D with the resources available nowadays both can be as easily learned in Unity - we are talking about "doing something".

    The magic behind learning a new language is to set tiny incremental but achievable goals, which avoids getting stuck and loose motivation, which can be very frustration. If the goals are tiny enough it allows you to also google the solution. "How to make a game" is something which will be too broad and not be easily followable for a newcomer, but "How to write your first .net console application" is something which yields tons of tutorials you can follow step by step. "How to write a loop in c#", "How to read from the console in c#", "How to write to a file in c#" - these are all questions which allow you to progress on your own, just with the resources already available, as many others have walked these steps before you.
  6. Yoreki


    Apr 10, 2019
    No, copying is not a good teacher, if done incorrectly that is! I would never ctrl+c --> ctrl+v code, that doesnt help. On the other hand, following a tutorial by typing down the code yourself is actually a great way to start. Especially so if the tutorial does a good job explaining what it does, so you actually know what you are writing on at any given moment. That way you can start experimenting and making little changes to get a better feel for how things work.
    However, i would start with the fundamentals before doing tutorials.

    Every programming language has at the very least some way to store information, jump conditionally, and write or overwrite memory. The last part is normally done under the hood, so you dont need to worry about it.
    (If you want to stick with Unity before needing it, just create some script and do the below in the Start() method)

    Let's start with variables. Variables are containers of a specific type you can store information in. Imagine them like a jar, on which you can put a label and then put in a specific type of cookies, later access this jar again to eat one, or put in more. Variables have a type, a name and a value.
    The most important types for beginners are: string, bool, int and float.
    Strings are used to store strings of characters, ints are used to store whole numbers, floats are used to store floating point (decimal) numbers. Bools are used to store truth values, so a bool can only be true or false (ignore for now, more on that later). A good point to start is to get comfortable with creating, assigning, reassigning, and working with the values in some variables. To do that, maybe solve some simple math problems by having numbers in different variables, and assigning the result to a new variable. That way you also look into common operators, which is helpful. The most common ones are +, -, /, and maybe % (modulo). Also, look into the interaction between int and float. What happens when you divide an int by something, since ints are only whole numbers? Just experiment around and see what you can learn.

    You can print information to the console using Debug.Log(something). Start by printing a just a single number. If you feel comfortable with your above number-experimentations, maybe print a nicer message by giving Debug.Log() a string you build yourself. You can concatenate string with other strings or numbers using the + operator. You could print for example: "The result is 5!", with the number being different depending on the actual result.

    After you feel comfortable with that, i recommend looking into methods / functions. A method is something you can call to execute some part of the code. For that look into how methods are declared. Important keywords are parameters and return types. Then maybe try to improve your math calculations by introducing your own methods for Add(), Subtract(), and maybe Power() or Sqrt().

    While we are at it, you should probably follow common coding conventions from the beginning to not learn bad habits. As default, you want to name everything in camelCase, using lower letters for everything, then starting each new word after the first with a capital letter. Methods, properties and class names are written in UpperCamelCase, which is basically the same but starts with a capital letter to mark the difference.

    Now that you feel comfortable with variables and methods, i would suggest looking into conditions. Often you only want to do something is some condition is true, or potentially do something other if that's not the case. For example, you could add a condition to your math functions that prints some warning to the console, if the resulting number is negative, or zero, or whatever. You can do this with if-statements. As you may imagine, when working with if statements, bools are important. However, you dont have to write "true" or "false" yourself. You can simply write a statement that gets evaluated to true or false. For example, "x < y" is true if x is smaller than y, and false otherwise. New operators you may want to take a look at are ==, !=, <, >, <=, >= as well as && and ||. With those you can compare or combine boolean values to get a new boolean value.
    To extend on the above example, you could now print a string that says: "The result is 5! That is an uneven number!", where now also the word "uneven" or "even" gets printed depending on what the result is.

    The above i would consider the basics, but you can already programm a lot of things with it. Now it would be time to learn about the single most useful tool (of the basics) you have as a programmer: arrays and loops.
    Arrays basically allow you to declare a fixed amount of variables of the same type. So an int array of size 100, is basically the same as 100 single variable declarations of type int. However, you can access these variables using their index. A loop can then be used to iterate over all array elements and, for example, add them all together, or write them all to the console, or do something else with them.
    Start by looking into arrays. Then look into loops, then combine both to get a general idea of how they work. There are different types of loops, the most important one is the standard for-loop. For now i dont think you need to look into other loops, as basically everything can be done with the standard one and the others just add conveniences or lazyness to some special cases. Examples for this would be the do-while or the foreach loop.
    Later you may want to have a dynamic array size, for which you can use the List<> wrapper type.

    The above basically allows you to programm anything, but there is still tons and tons more to learn. Most modern languages are object oriented, so you need to understand what objects are, what classes are, how classes can relate, that an instance of some class has its own state, how to modify that, and so on.
    Also, there is still basically the entire Unity workflow to learn, but you now have a good beginners understanding of how to get the executable code done, which is the most important thing for now. To sum it up real quick; Unity uses scripts as components you can add to GameObjects to give them new functionality. One component (script) could, for example, handle movement based on user inputs. Any GameObject you give this script to, will then move based on user inputs. Another script may shoot bullets from the position of the objects. Any GameObject you attach that script to will then shoot bullets from their position. And so on. That's the general idea, but of course there is more to it.

    If you want a tutorial series to walk you through all of this, and is also focussed on practical application in Unity, then i can recomment the one done by Sebastian Lague, which i'll link below. It's really good. Decide for yourself if you rather want to follow a tutorial series, or look into the topics for yourself. Now you can hopefully do both with the above description and the below tutorial series (26 vids). Wish you best of luck! :)
    Last edited: Oct 6, 2019
    Skaggsoft and MNNoxMortem like this.
  7. ItzChris92


    Apr 26, 2017
    Whilst copying is not the way forward, I found a good way to learn was searching for what I needed and finding a simple script for it. I would then replicate it and try to run through the code and fully understand what each part/line was actually doing. Use this in conjuction with the unity docs and you can't go far wrong.

    From there you can practice writing your own scripts from scratch. You'll undoubtedly make mistakes and your code won't always work as you expect, but you will learn a lot from debugging and fixing your mistakes.
  8. Suddoha


    Nov 9, 2013
    I'd say that seems to be a common problem for beginners, which does not only apply to programming.

    When I attempted to start programming more than a decade ago, I had a few C++ books and the Borland C++ Builder IDE.
    Tutorials from a book for that seemed so useless and contextless, it kind of bored me and even scared me away from it. I lost interest in porgramming really quickly.

    Similarly I followed some early Unity coders on YouTube back in 2008 or 2009. Even though I was fascinated about what they were doing, there seemed to missing something and I also couldn't get a hang on it.

    Later, I visited a school and we had a great teacher who teached us in a way that seemed perfect for my personal success in getting interested.

    So the difference was basically the order of things, and how big the "usefulness" or fun factors of those examples were...

    So up to that point, I always tried:
    - deep explanation of a concept, followed by numerous generic examples ... and now what??? Boredom, no clue where to go from here

    Then, I tried #2
    - specific, practical and sometimes very incredibly funny and absurdely funny examples => brainstorming => short idea of the actual programmatic concept => later a litte more abstraction

    There's no way you can always avoid 1), especially later when you need solid understandings of details before you're even able to understand the actual matter. Some people can generally learn that way from the beginning, but for many that's not efficient.

    Personally, the second approach helped me to learn incredibly fast. Suddenly I felt like a sponge that sucked up all the available information, as if I simply had an extra memory reserved just for that.
    Nowadays, when I read something new, e.g. about the new language features, I always associate it with a useful usecase or at least I try it out and see what I can do with it. It's just there afterwards, there's no real effort to learn it, for me it couldn't be any more efficient. Unfortunately, this usually only works for me when it's something that is related to programming. :p

    Long story short. It looks like you're that person who should approach the matter the other way around.

    Don't try to consume too much theory at once. Don't follow random tutorials just because they look promising.

    Instead, take a simple example of what you may need, and what you ever wanted to know / try out.
    Take an idea or area of interest, break it into small tasks and figure out the requirements (there will always be requirements, no matter what you'll be going for).

    The idea is to generate some sort of feelings like happiness and pride, i.e. you need to have fun, quick success. Sometimes even embarassment or anger can help, let it be akward failure during that process, e.g. when you do silly mistakes you never really wanna do again. Because that's what we remember, that's what we associate information with, that's how it generally works and that's how you keep up your motivation to improve upon yourself.
    Last edited: Oct 6, 2019
  9. Owen-Reynolds


    Feb 15, 2012
    "Create with Code" is what's known as a Survey. You're watching someone else make a game. Nothing wrong with that -- if gives a rough idea of the parts you'll need.

    Copying code is very traditional, but don't think of it that way -- you're taking apart a project to see how it works. There are some text-based Unity projects out there that you could try to tweak to do what you want. But if you don't know programming at all, you'll probably get bogged down(*). At that point, if you're still interested, read any textbook on variables, if, arrays, classes, and how they do things.

    (*) And/or that project could be stinky, which you may not know enough to realize. But anything working is still useful.
  10. Lethn


    May 18, 2015
    I highly recommend getting a glossary of some kind filled with programmer vocabulary to help you learn, that's what helped me when I was learning code. Very often a lot of these 'beginner' style courses assume a basic knowledge of programming most people don't know about so when they started talking in programmer gibberish which helps no one.

    Now thanks to learning about the vocabulary properly I'm at the point where I'm looking at custom shaders and algorithms to help me implement my game ideas. I think that's likely the OP's problem as well and what ends up frustrating a lot of beginners.
  11. Owen-Reynolds


    Feb 15, 2012
    Sure, Unity shaders use lots of funny terms, but that's much later. How much vocabulary do you need starting? Maybe 1) syntax = spelling, semantics = what it does, 2) exception = error, 3) argument is what you send, parameter is what it gets, 4) index = number starring from 0 used as a look-up. I can't think of much more, and you see those over a long period.
  12. alzakielz999


    Oct 2, 2019
    Well that's a lot of answer and interesting thing! I've read all of you and i'll be looking into this. I'm sure some has more to say, so don't mind giving your own answer. It's kinda interesting at the sametime how people go about this.

    I won't be doing much today sadly, i've stayed up till 5AM last night trying to read how i could do Yes or No selection into console for which clicking ''N'' would close the application and ''Y'' would do nothing since there isn't anything after that for now.

    4h of sleep isn't enough lol...