Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

C# Script

Discussion in 'Editor & General Support' started by games28, May 28, 2016.

  1. games28

    games28

    Joined:
    Dec 31, 2014
    Posts:
    32
    I would appreciate if anybody could help with this simple problem. I’m learning Unity script following a book and although I copy the script exactly as it appears on the book the Unity editor says there are compile errors and the script cannot run. I’m wondering if that is because Unity uses visual studio instead of Monodevelop that is used in the book.

    Here is a screen shot of the script.

    upload_2016-5-28_23-56-14.png
     
  2. Maurdekye

    Maurdekye

    Joined:
    Jan 15, 2015
    Posts:
    4
    You should really learn C# by itself first before trying to use it in unity. There are lots of online resources like Codeacademy and other tutorials you can use to learn C#.
     
  3. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    What's the error?
     
  4. Eudaimonium

    Eudaimonium

    Joined:
    Dec 22, 2013
    Posts:
    131
    The error is that he's trying to write an integer number into a Debug.Log function which expects a string.

    Correct way to output your result would be,

    Code (CSharp):
    1. int myResult = 11 + myNumber;
    2.  
    3. Debug.Log(myResult.ToString());
    Or, shorter but less readable way,

    Code (CSharp):
    1. Debug.Log((11 + myNumber).ToString());
    @games28, Here's a tutorial series from which I started:
    http://www.homeandlearn.co.uk/csharp/csharp.html

    I would also recommend trying to learn a bit of XNA / MonoGame coding and get a solid grip on a few concepts there. Then try Unity scripting.

    While I'm all for "learning by doing", scripting logic within a game engine does require having a few basics down first.

    EDIT == Oh and, also, when you say "but it just throws an error", you are REALLY going about problem solving the wrong way around. Error messages are your best friend to figuring out what's wrong, and most commonly they tell you how to fix it! Read the error messages!
     
  5. games28

    games28

    Joined:
    Dec 31, 2014
    Posts:
    32
    Thanks all of your for your help. You are right that I should learn C# first before going to Unity. But I didn't know that there are free online courses out there for C#.
     
  6. Eudaimonium

    Eudaimonium

    Joined:
    Dec 22, 2013
    Posts:
    131
    There are free online courses for everything in between making a lifesize Batmobile in origami, to 10-meter arc solid state Tesla coils.

    You can be sure there are a few good programming resources out there :D

    My advice, for basic programming tutorials, stay away from youtube. Congratz to exceptions, but people generally are very bad at objectively explaining logical contents. Instead of "this is how it is", it turns into "this is how I see it", which usually devolves down into "just write this, doesn't matter what it means, it has to be there".

    Youtube tutorials generally get OK at solving a particular problem or something of a "narrow field" though.