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.

Problem with learning C# in Unity

Discussion in 'Getting Started' started by Simulacr0n, Nov 13, 2016.

  1. Simulacr0n

    Simulacr0n

    Joined:
    Nov 13, 2016
    Posts:
    12
    I have a problem with the coding in Unity. I watched multiple youtube series on how to code in Unity and while they explain the code to me I understand everything. I also can roughly interpet code from people that don't explain that much. However as soon as I want to add my own code I'm stuck. I have no idea where to start and how to do it. My problem is that I don't really know what commands I need to use and how the syntax of the code exatly works. I also already programmed before with the tool gamemaker and think here lies part of the porblem, because this code also has the basic logical structure but uses a different syntax, so now I can't rethink to Unity. How did you learned the syntax and coding itself? Thanks in advanced. (Also sorry for the bad English)
     
  2. mushey

    mushey

    Joined:
    Nov 13, 2016
    Posts:
    39
    man i have similar problem and i dont know where to start like ive watched a lot of videos and done alot tutorial but when i want to make somthing i dont know wt to type
     
    yaxman likes this.
  3. Simulacr0n

    Simulacr0n

    Joined:
    Nov 13, 2016
    Posts:
    12
    Yeah, I feel exactly the same, anybody has some tips for us?
     
    yaxman and mushey like this.
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Syntax is made to be looked up. No need to memorise it.

    First start with drawing the logic out on paper.

    Here are some questions to ask
    • What should the script do? This becomes your methods.
    • What data does your script need to do it? This becomes your variables.
    • What do other scripts need to know about this one? This becomes your public variables/methods.
    • When should this script activate? This becomes your Awake/Start/Update ect.
    Once you have the structure and logic written down, open up the default template from Unity. Add in your variables (google syntax for each one). Add in the code inside of Start and Update. Test and fix errors frequently. Google as you go.
     
    mushey, Ryiah and yaxman like this.
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    18,376
    mushey and Kiwasi like this.
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,819
    The other thing you must learn to do is to break a big problem up into smaller problems.

    Many times a beginner will start with something like "I want to make this turret turn and shoot at where I click." But this is too much for a newbie to bite off all at once. You have to break it up into smaller problems, and keep doing that until you get tiny little pieces that you can focus on one at a time. For this example, it would be things like:
    • Detect when the mouse is clicked at all (just Debug.Log a message to prove you've done it)
    • Figure out what position it the world corresponds to the position of the mouse
    • Make the turret point at a certain world position
    • Spawn a prefab when clicked
    • Make a projectile that moves forward
    • Make a projectile go away (destroy) when it goes offscreen, or after a certain amount of time
    • Make a projectile do damage when it touches a damageable object
    Etc. THESE are tasks small enough for a newbie might actually be able to find or figure out how to do. The original goal is just the combination of all of these, but is too much to tackle in one go.
     
    mushey, Kiwasi and Ryiah like this.
  7. DiamondDog

    DiamondDog

    Joined:
    Aug 2, 2016
    Posts:
    1
    I’m no expert - to put it mildy - but I’ve dabbled in a bunch of different languages over the years and I’m now getting back into C# for Unity games programming.

    The best tip I can offer you is to start small. In fact, to start with, I’d write some simple ‘console’ apps which write their output to the screen. For example, can you write a method which adds two numbers together? What about a method which tells you how many vowels a word contains? A method that tells you whether a given number is prime? Can you write a console app which employs a simple class which has no more than a couple of methods? Once you get confident with that kind of thing, then’s the time to start controlling game assets via code. Just a suggestion. Good Luck.
     
    mushey likes this.
  8. mushey

    mushey

    Joined:
    Nov 13, 2016
    Posts:
    39
    Thanks for ur awesome reply, so what would u do if u wanted to do somthing that no one has done.
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Get out of programming. There are almost no problems in programming where the pieces aren't already solved for you. Sure, the specifics might be new, but that's details.

    :p
     
    Ryiah and mushey like this.
  10. mushey

    mushey

    Joined:
    Nov 13, 2016
    Posts:
    39
    Okay so i dnt know if anyones currently doen this or not and im new to unity--- how would u make a 3d type of menu where when u click start ,the camera spins around the menu and then it goes down and u have sub menu option e.g level 1 level 2
     
  11. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    18,376
    Spinning the camera around the menu is as easy as attaching the camera itself to another gameobject. The gameobject will act as a pivot point for the camera. You simply position it where you want the center of the rotation to be. Keeping the camera pointed at the pivot can be achieved with the following API call.

    https://docs.unity3d.com/ScriptReference/Transform.LookAt.html

    Anything more complex can be implemented in several ways such as with coroutines.



    That said if you want a visual approach you can use a spline tool from the asset store. Here are two examples.

    https://www.assetstore.unity3d.com/en/#!/content/58080 - Spline Editor - $7
    https://www.assetstore.unity3d.com/en/#!/content/617 - Camera Path Animation - $30
     
    Last edited: Nov 14, 2016
  12. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,819
    You could also do this with simple Unity animations on the camera itself (or the camera container, if you've set it up as @Ryiah described). Just make an animation for each choice, and then trigger the appropriate animation when an option is selected.

    Easy peasy, and requires little to no coding.
     
    Kiwasi likes this.
  13. mushey

    mushey

    Joined:
    Nov 13, 2016
    Posts:
    39
    Thank you
     
  14. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    4,322
    That's where you begin your great adventure! You'll have to train your creativity and get advice from people who know coding better than you do. There's nothing to be ashamed in asking for help. ;)

    (What's wrong with writing you and your entirely?)
     
    mushey likes this.
  15. mushey

    mushey

    Joined:
    Nov 13, 2016
    Posts:
    39
    Sorry thats how i type sometimes....
     
  16. Simulacr0n

    Simulacr0n

    Joined:
    Nov 13, 2016
    Posts:
    12
    Thanks for all the repilies! However I have another qeustion. Is there any list of these things like this this.transform.parent.gameObject, I have no idae how this chains are called and if I'm programking I don't know which to use, because I just know a few from hte tuorials. I understand the logic behind the programms but don't now in which commands. For example, I know that I need to store the mousemovment in some sort of varible to move a camera to create a fist-person-view, but I don't know how to express these statements (Find mouse movment - aplly them to the x-rotation od the camera,...).
     
  17. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    18,376
    Let me break that statement down for you. Each of those words refers to a property or variable for its type. The first word being used (this) is simply an easy way to refer to the instance of the script itself. It's basically syntactic sugar in that you can refer to the script instance in other ways but this is the easiest.

    Since the script is a MonoBehaviour we have to look at the document entry for that class next. If you look under the Variables section of the entry you'll notice the next word (transform). It is referring to the transform component of the game object that the script is attached to. The type of that variable (transform) is Transform.

    https://docs.unity3d.com/ScriptReference/MonoBehaviour.html

    If we look at the Transform entry of the documentation we can find the next word (parent). It refers to the transform of the game object that is the parent to the game object the script is attached to. If the game object does not have a parent it will hold be null (the programming way to say nothing is stored in a variable).

    https://docs.unity3d.com/ScriptReference/Transform.html

    Once again we look at the Transform entry but this time under Inherited Variables and we find the last word (gameObject). It's simply referring to the game object that the transform (in this case the parent's transform) is attached to and is returning it.

    Now we could have done that entire process one stage at a time if we really wanted to with the following code.

    Code (csharp):
    1. Transform t = this.transform;
    2. Transform p = t.parent;
    3. GameObject g = p.gameObject;
    We could even leave off the syntactic sugar because the compiler assumes by default we're referring to the script's variables.

    Code (csharp):
    1. Transform t = transform;
    2. Transform p = t.parent;
    3. GameObject g = p.gameObject;
    It's just far easier to read this.

    Code (csharp):
    1. GameObject g = this.transform.parent.gameObject;
    Or this.

    Code (csharp):
    1. GameObject g = transform.parent.gameObject;
     
    Kiwasi and JoeStrout like this.
  18. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,819
    Those chains aren't incantations that you just memorize and then invoke, like casting spells. They're more like LEGO bricks: you select the ones you need and combine them however you want to achieve your goal.

    In that example, you had some reason to need to get the parent GameObject... maybe you knew your script was going to be in a child object, but when it's hit you need to deactivate at the parent level. So how do you get that? Well, you start with what you have. You have a transform (every component does). So, "transform" (note: you don't need "this." here, as everything in the scope of "this" is available by default anyway). Transform has a parent property, which gives you the Transform containing this one. So now you have "transform.parent". But you wanted a GameObject, not a Transform, so you use the gameObject property (which Transform inherits from Component), and now you have "transform.parent.gameObject".

    If you needed to go one level further up in the hierarchy chain, then it'd instead be transform.parent.parent.gameObject. If you needed a sibling object (i.e. another object with the same parent as this one) called "Foo", then it would be transform.parent.Find("Foo").gameObject. There are infinite combinations possible, just like with LEGO. Assemble these parts however you need!

    In the second part of your question above, you are again trying to bite off too much at once, just like I described above. Break it into smaller parts, and then focus on just the tiniest part at once. In this example, it would probably be the "find mouse movement" part. Can you display the X component of the mouse movement in a Debug.Log? If not: can you display anything in a Debug.Log? If not: can you create a new C# script and add it to an object? If not, go do some tutorials. :) If so, start with what you can do, and build from there, one step at a time. Don't worry about subsequent steps.
     
    mushey, Kiwasi and Ryiah like this.
  19. Simulacr0n

    Simulacr0n

    Joined:
    Nov 13, 2016
    Posts:
    12
    Thanks again for all your help, next time I programm something, I will think about your tips!