Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Start scripting

Discussion in 'Scripting' started by DirtyBlasion, Sep 5, 2016.

  1. DirtyBlasion

    DirtyBlasion

    Joined:
    Aug 21, 2016
    Posts:
    31
    So I want to start with c#, in the past I worked with game maker studio, and I have an idea of programing.
    Here are my questions:
    1) When I use public?(I know I use this to define elemnts at the beggining of the script, but in other cases I seen public bla bla {bla bla}, but in this case I think is to set some values, if is true what I said, then when value is changed, per tick? Or at script start?)
    2) When I use void?(live void blabla (){bla bla bla})
    3) When I use public void ...?(I have seen to some scripts public void bla bla{bla bla bla})
    4) I heard there are c, c++ and c#, what are the dif and what they use better?

    Edit: When I can use return, and what that does, and what value should do? From it's name, "return" I belive this repeat script, it's true? If not, tell me please

    And the rest I know.
     
  2. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    855
    You need to learn c# at first before you start to learn unity
    void ,return ,... is the fundamental key words in programming language. you can read a C# book or a lot of forums about that
     
  3. DirtyBlasion

    DirtyBlasion

    Joined:
    Aug 21, 2016
    Posts:
    31
    I read, but I dont understand... I know a lot of things but these blocks me... I want just some dot and at the object(in my language is expression of a short and concrette answer) to say to me and I can just jump into programing, thats all I need
     
  4. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    public is just a tag that says other things outside this script file can "See" this object.
    if I have this:
    Code (CSharp):
    1. public class MyClass {
    2.         public int SomeInteger;
    3. }
    4.  
    5. //Somewhere in another file
    6. // this will work.
    7. MyClass  myClass = new MyClass();
    8. myClass.SomeInteger = 5;
    9.  
    10. //However if you remove the public from SomeInteger
    11. public class MyClass {
    12.         int SomeInteger;
    13. }
    14.  
    15. // Then in another file:
    16. MyClass  myClass = new MyClass();
    17. myClass.SomeInteger = 5;  <-- The compiler will throw an error You can't see non Public variables in another file
    Functions (Methods) are bits of code that perform a job. When they are done performing that job , it can either be over, or it can return an answer if needed. You have to tell the compiler which is it. Void tells the compiler that this method will not return an answer
    Code (CSharp):
    1. void SomeMethod()
    2. {
    3.      /// do some stuff in here
    4.      /// when we are done we don't return any answer
    5. }
    6. //Notice I put int instead of void
    7. int SomeMethod()
    8. {
    9.            // do somestuff in here
    10.            // when we are done we got an integer answer
    11.           return SomeInteger;
    12. }
    C , C++ and C# are just different programming languages. Out of those three, Unity Scripts can only be written in C# so ignore c, c++. Just find tutorials on C#

    Note: none of those tags have anything to do with actually changing variables or when they are changed. You do that on your own.
     
    Kiwasi likes this.
  5. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    you got to look at some basic learning content mate, you need to learn what protection levels are (private, public, protected) and about types and what void makes.
     
  6. RickyX

    RickyX

    Joined:
    Jan 16, 2013
    Posts:
    280
    Yeah fam, not to be mean but programming is a huge thing. You don't know even the basics of C#. I recommend reading books about C# as @mahdiii said, watch tutorials, read forum posts if you have questions about certain things/problems, but all of this BESIDE unity. When you understand the basics, you're ready for unity. The way i learned C# is very weird and i do not recommend it to anyone. It took me years to learn C# just because i took the most difficult way, which is going straight into unity and trying to understand "complicated" (at that time it was complicated for me) code, trying to make my own while i knew almost nothing about basic programming, it was just horrible... And the stupidest thing is that I couldn't realize that my way of learning was not a right way haha. But oh well, that's why i'm telling you man, this is not the right way, it's a waste of time. I think many will disagree with me though, but this is from all my honesty, from my experience.
     
  7. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    855
    Dude you only waste your useful time here. The best advice is to read basic c# ebook and then watch movies(toturials). After that you understood the basics come to C# forums:
    1-https://msdn.microsoft.com
    2-http://stackoverflow.com
    3-https://www.quora.com
    4-Reddit.com/r/programming

    and ask your questions, The most question Here is about untiy game engine scripting.
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You've got two questions muddled up here.

    1) What is an access modifier?
    - An access modifier (public, internal, protected or private) specifies what code can access the member. For more reading Google encapsulation.

    2) How do methods work?
    - A method is a reusable snipet of code. Control can be passed to the method by invoking it. A method passes control back to the caller by calling return (or by running out of code). A method can return a value, or it can return no value (void).