Basic programming

Hi I am trying to get to grips with basic C# programming so i can follow peoples scripts etc but the language is so convoluted and nonsensical not to mention the absurd use of CaPiTiliZation!!! and punctuation.:'!!@#@!@"@@!###@!#%#%^@#%^@%^ I’m struggling right off the bat.

I noticed people don’t really reply much on these threads so I’m hoping some kind soul might assist me.

This is tutorial 2 in the unity basic scripting lessons. Far as i can tell there are no values assigned to anything other than myint.
Here is the script

using UnityEngine;
using System.Collections;

public class VariablesAndFunctions : MonoBehaviour// name of script
{
int myInt = 5;   //assigning an integer value of 5 to myint

void Start ()
{
myInt = MultiplyByTwo(myInt)  ;    //mutiplybytwo has no given value so its basically saying myint=myint
OR 5=5
Debug.Log (myInt);  
}


int MultiplyByTwo (int number)    // here not a clue mutiplybytwo has no given value and number has no given value anywhere
{
int ret;   //another int coming called ret why another int isn't 3 enough already!!@#$#@@!@##@@!
ret = number * 2;     // there is no value assigned to number!!! wtf
return ret  //return ret to where?
}
}

Somehow this mess returns a number 10 which is myint*2 This is gibberish to me.

If I am reading this correctly, this is what is happening:

  1. myInt is available to the class and initialized to 5.
  2. The Start method runs when the class is created at uses the defaulted value of 5.
  3. Start also calls the MultiplyByTwo method supplying myInt, then it is multiplied by two and returned.

if you want to learn C# from scratch there are much better ways than doing unity tutorials. If I interpret your questions correctly you have no prior knowledge about programming at all. In that case i would suggest you look into learning some basic (normal) programming first.
http://csharp.net-tutorials.com/ has some nice tutorials to get you started

Thx mate not a clue what you mean but ty for the response i’m sure these’s a easier way to write this code seems overly complicated for a simple problem

Be a lot easier to help you if you used proper code tags here.
“MultiplyByTwo” is a function which multiplies the supplied int (in this case, myInt) by 2, not a variable thus it needs no “value”. Only needs to be called which it is.
Yes, this could be done quite easily by just multiplying by two in the start function, but I suspect the whole purpose of that tutorial is to teach you the difference between a variable and a function.

I grew up with B.A.S.I.C it is a wonderful language that makes sense doesn’t throw in obscure punctuation brackets capitilization etc but yea not a clue about c# and java looks even worse I will go have a look at that site but just trying to get a grasp and more alter modify scripts than write from scratch I can’t get the hang of . ; : { }( ) HeYrAnDoMcApItAlS its illogical. Unfortunately all the scripts i copy paste from here don’t work so its hard to break them down for study when they broken already . Thx for the response

Not following mate whats the reason for the int number and int ret if its all done already in the multiplybytwo. I think i’m just too stupid to get this.

There are sound reasons why it is a case sensitive language but I agree initially it can be intimidating to write or modify code in these types of languages.

One way to condense to use fewer variables (a little bit easier on the eyes) would be to do this:

using UnityEngine;
using System.Collections;

public class VariablesAndFunctions : MonoBehaviour// name of script
{
int myInt = 5;

void Start ()
{
myInt = MultiplyByTwo(myInt);
Debug.Log (myInt);
}

int MultiplyByTwo (int number)
{
return number * 2
}
}

Basically I think the previous recommendation to work your way through http://csharp.net-tutorials.com/ would be a great starting point for learning the language outside of a Unity environment (anything you learn could then be applied simply to Unity). It might even be worth picking up a “Learn C# in 24 hours” book or “C# for Dummies” (as much as that name is terrible). There are numerous online resources for learning the language at an absolute fundamental level that would benefit you a great deal. I hope you keep up trying to learn as it is massively rewarding.

Good luck!

Hey bud yea I’v started to look at the site mentioned you did clean up the code a bit also thx still seems to have too many int’s to me the "int number: seems unneeded
This line confuses me most
myInt = MultiplyByTwo(myInt); there is no + - / or * or = value or do the brackets have some mathematical meaning in this case multiply?.As the end outcome is 10 mutiplybytwo can only be the number 2. .

And this line
int MultiplyByTwo (int number) number has no given value that I can see(it should be 1 if the maths is to work right) so how can it later be multiplied by 2. 0*2=0 .

I like the idea of learning c# in 24 hours but i fear that might be an extreme misrepresentation or a rather optomistic view of the human brain’s ability to absorb gibberish on the books part. The dummies book sounds more my speed lol but it would probably be referring to a more intelligent dummy than I. I shall go through that online tutorial as well as read scripts here in unity for this week see if I make any headway.

The C# in 24 hours is not literally 24 hours, its a blanket term for a series of books that breaks learning new languages/skills down into multiple lessons (usually an hour in length) that try to teach you the fundamentals in about 30 exercises.

I’ll try my best to explain some of the things you are struggling to understand though.

The line myInt = MultiplyByTwo(myInt); is basically setting the value of the variable myInt to be equal to whatever the MultiplyByTwo function returns. In this case the function will return whatever value you pass to it, which brings us to the next line you don’t understand.

int MultiplyByTwo (int number)

This line is declaring a function that has the return type of Int (this is the type of data it will return with the return statement), the declaration also specifies any parameters the function takes in this case the function takes an integer called number. The actual maths happens inside the function, when you call the function in your code you pass the value of myInt to the function. This essentially means number = myInt, however the two aren’t linked so any changes to number won’t affect myInt until you return something. Then whatever you return becomes the value of myInt.

That might not be the most clear explanation but it should explain exactly what is happening in the code. A fantastic tutorial for functions can be found here; Methods - C# | Microsoft Learn

Hope some of this helps! :slight_smile:

The line

  • myInt = MultiplyByTwo(myInt);

is basically setting the value of the variable myInt to be equal to whatever the MultiplyByTwo function returns. In this case the function will return whatever value you pass to it, which brings us to the next line you don’t understand.

Ok that makes sense cept for the way its written out myint=multiplybytwo(myint) the (myint) at the end is what confuses me its already been established that myint=multiplybytwo so the bracket(myint) seems unnecessary.

  • int MultiplyByTwo (int number)

This line is declaring a function that has the return type of Int (this is the type of data it will return with the return statement), the declaration also specifies any parameters the function takes in this case the function takes an integer called number. The actual maths happens inside the function, when you call the function in your code you pass the value of myInt to the function. This essentially means number = myInt, however the two aren’t linked so any changes to number won’t affect myInt until you return something. Then whatever you return becomes the value of myInt.

Nope still as confused as ever don’t see why the number 5 is given to the int number and not to the int multiplybytwo no where (i can see) does it say let int number=myint .I think I will avoid functions like the plague I’m sure I can code without having to resort to this tomfoolery.
As far as the tutorial website is going I pretty much understand what i’v seen up to loops where I’m at but the brackets and punctuation confound me I would need to have examples of sentence structure open anytime I need to code anything it seems.
Thx for the explanation though
The biggest difference I see between C and B.A.S.I.C apart from the punctuation!!! is no goto command for a line of code reference which seem to necessitate all these squiggly lines{ }. I say make unity compatible with B.A.S.I.C!!! never even heard of that 3rd language they use.

The reason you need brackets is because its a function call and not a variable, it needs the parameter to be passed. Because myInt = 5 and number = myInt when it is passed through the function that means number = 5. Functions are a pretty fundamental part of modern programming and C#, they are slightly more fundamental than Objects (a far more complex topic that you’ll come across later).

The differences between BASIC and C# are massive, there are lots of things to consider initially but a lot of the issues you are having with the language now will go away after you write code for a decent period of time. If you are wanting to read documentation on anything relating to the language I’d recommend the MSDN resources for C#, they are amazing (and free). This is a pretty good tutorial on functions in C# Introduction to Functions in C# it might be worth taking a read of that.

Hope this helps a bit more.

EDIT: Just a quick sidenote, are you talking about original BASIC (BASIC Programming/Beginning BASIC/PRINT, CLS, and END - Wikibooks, open books for an open world) or Visual Basic (Visual Basic - Simple English Wikipedia, the free encyclopedia)? This would help any further help people might be able to give.

Okay, i’ll give it a try. I’m German though, can’t tell you all the correct technical terms.

The following line is a simple variable:

int myInt = 5;

The first thing is the type, the second is the name. Moreover there can be modifiers for different uses.
I think that’s clear, now let’s start with a basic function in C#.

Functions

void SayHello()
{
      Debug.Log("Hello World");
}

A normal function helps to write re-usable code, you can call it in your code with a single line so that you do not need to re-write all the code which you put into the function’s ‘body’. Additionally, the list of paramters makes it even modular and re-usable even more, which is a great thing you would not want to miss once you understood the principle. Believe me, it saves lot’s of lines => work => time, even in small projects.
Made a logical mistake? A calculation was implemented wrong? Maintain your function and it will most-likely be fine. That’s actually the best thing, you don’t need to waste time searching all the mistakes of copy/paste code.

Return type
Now, the first thing a function usually has (there are special ones such as constructors which do not have one) is the return type. This can be void, int, float or any other basic or your own type (data/reference types → see below).
In my example, it is void, so nothing will be returned and the return-statement is not neccessary.
The function does it’s task and whenever it’s done, the programm will continue at the point at which it has jumped ‘into’ the function.
If you choose to have a return type, the function needs to have the return-statement and a valid variable or a constant ‘hard coded’ value. (in German it’s also called Literal, not quite sure whether it’s the same in english or not).
With a return type, the function can be used similar to variables.

  • assign the returned value to a variable
  • use it directly, i.e. in a calculation

Name
Second part is the name. You should always take self-descriptive names so that other people who take a look at your code can follow along alot easier + it will help you alot, too.

List of parameters
The third part which seems to be the most confusing to you is the list of parameters. Those are local variables of the function which will only be available in the function itself! The above example hasn’t got any paramters, so let’s add one.

void SayHello(string name)
{
  Debug.Log("Hello " + name);
}

Now, whenever you call this function, you will need to pass in a valid variable of the type string or some ‘hard coded’ string as in my example below. (Okay, there’s still overloading, but that rather counts as intermediate).

Let’s say you call it in the start function

void Start()
{
    SayHello("Suddoha");
}

The string will be passed to the function and the function will do whatever is defined in its body.
One thing you need to know by now is that there basically two different types of variables:

In C#, types such as int, float, bool, any kind of structs etc are data types which means they ‘really’ contain data.
Reference types are variables which actually contain a reference to an instance of a class,it’s a bit different there.
Right now it’s enough to talk about data types in order to understand the code you posted.

Data types:
If you were about to assign an int variable to another int variable, you would actually copy the value it contains. Thus you can alter the second int without changing the first int.
That’s what happens in your example. An integer is passed to the function and can be accessed by ‘number’, actually it will be copied this way and using 'number’ will not affect ‘myInt’ The function multiplies ‘number’ by two. At this point, ‘myInt’ is still 5, ’ number’ is 10 - remember, integers are data types and thus treated independantly. (Ofc, there’re ways to make this work differently, but that’s far away from beginner skills).

Since the purpose of that example script is the calculation and reassignment of the class variable myInt, you need to use the value which is returned by the function. Remember, we can use the function just like a normal variable in this case.

1 Like

I hope you aren’t trolling. Since if you know basic you would be at an age where you know where to get information from.
Quote(tawdry, Sep 3, 2014):“Hi guys I’m new to Unity3d hell i’m new to programming period.” When i press play is removes everything i just added - Unity Engine - Unity Discussions

Well even with BASIC there are functions which are similarly ordered as in C# and any other programming language i know of. Here you can see a function in BASIC BASIC Programming/Subroutines and Functions - Wikibooks, open books for an open world
Maybe it will help you to find similarities with the syntax of c#.

If you really already programmed in BASIC than you can start with http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/scripting-primer (Explainations about Unity specific class definition start at about the 12th minute) or Learn game development w/ Unity | Courses & tutorials in game design, VR, AR, & Real-time 3D | Unity Learn

If you want to go into more details in how to script you can follow tutorials that you can find on Getting Started with Coding for Unity - Learn Content & Certification - Unity Discussions and List of Tutorials and Resources - Learn Content & Certification - Unity Discussions ( Don’t copy and paste. Write the code line by line. Than you will see that nearly after each line “;” is put and {} encloses related code in function blocks. )

A nice simple tutorial is http://catlikecoding.com/unity/tutorials/clock/
From what i have seen. Every step is shown. If you read and follow it step by step you should be able to understand a bit more in how to use Unity. A bit more advanced would be the second tutorial http://catlikecoding.com/unity/tutorials/constructing-a-fractal/
There you see why you need functions. Otherwise you wouldn’t be able to create more complex behaviour.

To your multiply by two example.

int MultiplyByTwo (int number)  //4 words. 1st: return type integer; 2nd function name; 3rd parameter type; 4th parameter name which can be used in the function
{
   //dosomething with number and return the result
   int ret;  //define a return variable
   ret = number * 2; //do the math with number and input it in ret
   return ret //output the result which must have the same type as the function has standing before its name.
   //return number * 2; //if there is no more complex function this would be a shorthand which would skip the additional variable ret
}

{
   myInt = MultiplyByTwo(myInt);
}
{
//for easier understanding i'll write it different
   int resultInt; //just a variable to hold the result
   resultInt = MultiplyByTwo(myInt);
   //the function gets the parameter myInt. The return type of the function would hold the result which gets generated in the function.
   //put the return of function into resultInt
 
   the cause of the Capitalized letters.
   resultInt = Multiply By Two(myInt);  //this would not work. A function/variable/parameter and so on has to be lumped together.
   //Capitalization helps in reading and differentiating each word.
}

For more details about c# writing styles you can take a look at Some Best Practices for C# Application Development (Explained) - CodeProject

btw actually goto exist in c#. But for the beginning you should get used to normal functions/if-else and unity specific names. C# goto Examples - Dot Net Perls

**

[quote]**
I think I will avoid functions like the plague I’m sure I can code without having to resort to this tomfoolery…
**[/quote]
**

Yeah. Obviously a troll.

Will take a gander at it tomorrow late now and I just finished going through the first suggested tutorial. Got completely lost once it started talking about classes etc.Talking about old school BASIC sir. Had a zx spectrum as a kid and played around with that I doubt most here would have even seen one of them. .

iI’ll read through the explanation again tomorrow been a long day thx for all the resource material gonna be a very long day tomorrow again it seems.

ow mate you really went into details just glanced through it right now will have a more indepth look at it tommorrow with fresh eyes I think its the lack of symbols = + etc that really throws me off.

Just read the first few chapters of a book or follow any online tutorial for c# beginners. The things you are asking about are basic programming concepts that are always covered in detail in the first few steps of learning, because you can’t get anywhere without them. If you think you’re saving yourself time and not wasting anybody else’s by asking a forum to teach you the fundamentals, then you are wrong.

I tried this and did not understand I thought I’d try another resource (unity forum) and see if there was someone who could dumb it down for me. I apologize if my stupidity offends you it is not intentional.

Yeah. Obviously a troll.[/QUOTE]

I am most assuredly human sir though perhaps my visage might invoke comparison.