Search Unity

What wrong with void ??

Discussion in 'Getting Started' started by badassgamer, Jan 24, 2016.

  1. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    I got error in the code but as i began to learn i can slove most of them myself but this one i cannot . the error says "Assets/Targetting.cs(52,14): error CS0116: A namespace can only contain types and namespace declarations"
    Here is my Code
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class Targetting : MonoBehaviour {
    6.     public List<Transform> targets;
    7.     public Transform SelectedTarget;
    8.  
    9.     private Transform myTransform;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         targets = new List<Transform>();
    14.         SelectedTarget = null;
    15.         myTransform = transform;
    16.  
    17.         AddAllEnemies();
    18.     }
    19.     public void AddAllEnemies () {
    20.  
    21.         GameObject[] go = GameObject.FindGameObjectsWithTag ("Enemy");
    22.  
    23.         foreach (GameObject enemy in go)
    24.             AddTarget(enemy.transform);
    25.  
    26.     }
    27.     public void AddTarget (Transform enemy) {
    28.         targets.Add (enemy);
    29.  
    30.     }
    31.     private void SortTargetByDistance () {
    32.         targets.Sort (delegate (Transform t1 , Transform t2) {
    33.             return Vector3.Distance (t1.position, myTransform.position).CompareTo (Vector3.Distance (t2.position, myTransform.position));
    34.         });
    35.  
    36.     }
    37.  
    38.  
    39.     }
    40.  
    41.     private void TargetEnemy()
    42. {
    43.  
    44.     if (SelectedTarget == null)
    45.     {
    46.         SortTargetByDistance();
    47.  
    48.         SelectedTarget = targets [0];
    49.  
    50.     }
    51.  
    52. }
    53.  
    54.  
    55.     // Update is called once per frame
    56.     void Update () {
    57.         if (Input.GetKeyDown (KeyCode.Tab)) {
    58.             TargetEnemy();
    59.         }
    60.  
    61.     }
    62.  
    And here is the code from someone who says its work
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class Targetting : MonoBehaviour {
    6. public List<Transform> targets;
    7. public Transform selectedTarget;
    8.  
    9. private Transform myTransform;
    10.  
    11. // Use this for initialization
    12. void Start () {
    13.   targets = new List<Transform> ();
    14.   selectedTarget = null;
    15.   myTransform = transform;
    16.  
    17.   AddAllEnemies ();
    18. }
    19.  
    20. public void AddAllEnemies()
    21. {
    22.   GameObject[] go = GameObject.FindGameObjectsWithTag ("Enemy");
    23.  
    24.   foreach (GameObject enemy in go)
    25.    AddTarget (enemy.transform);
    26. }
    27.  
    28. public void AddTarget(Transform enemy)
    29. {
    30.   targets.Add (enemy);
    31. }
    32. private void SortTargetByDistance()
    33. {
    34.   targets.Sort (delegate(Transform t1, Transform t2) {
    35.    return Vector3.Distance (t1.position, myTransform.position).CompareTo (Vector3.Distance (t2.position, myTransform.position));
    36.   });
    37. }
    38.  
    39. private void TargetEnemy()
    40. {
    41.   if (selectedTarget == null) {
    42.    SortTargetByDistance ();
    43.    selectedTarget = targets [0];
    44.   }
    45. private void SelectTarget()
    46. {
    47.   selectedTarget.GetComponent<Renderer>().material.color = Color.red;
    48. }
    49. // Update is called once per frame
    50. void Update () {
    51.   if (Input.GetKeyDown (KeyCode.Tab)) {
    52.    TargetEnemy();
    53.   }
    54. }
    And those Extra thing like get color red are next step in the code but i cant even get this thing working so i didnt add that in my code because it needs to be added later when this get working
     
  2. Farelle

    Farelle

    Joined:
    Feb 20, 2015
    Posts:
    504
    (im talking about the line numbers in your script)
    line 34. both scripts have a bracket and semicolon behind a scope, I'm pretty sure that this shouldn't be and would throw an error.
    line 36. and 39. you have 2 scope ends there, while the other code has just one, is it maybe one too much?
     
    badassgamer likes this.
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,183
    Count how many opening brackets '{' and closing brackets '}' you are using. You only need one closing for each opening.
     
    Martin_H, Schneider21 and badassgamer like this.
  4. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    6,208
  5. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    Thanks for the help but this is bit strange because almost everyone made mistake in curlybraces but the error usually says "parasing error" but this time its says something different so thats the problem. if it had said "parasing error" i would myself sloved it lol
     
  6. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    You've closed out too many curly braces, so you're now outside the class declaration. The compiler expects anything outside a class declaration to be either a type or namespace declaration. You're declaring a method (TargetEnemy). This is not allowed, so at the end of the method declaration, the compiler says "You can't do that".
     
    Martin_H and badassgamer like this.
  7. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    I want know something about scripting but not about any errors but with this script so i can post it here or create another thread?
     
  8. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    For the love of God, please don't make a new thread every time you have another question... :p If it's related to the same script, just ask here.
     
    Ryiah and badassgamer like this.
  9. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    OK then i will post here. want i want to know that now i made the full script with working but while learning this script i had lots of area where i dont understand what the script means (i did it by watching tutorial) but with great diffculty just got it understand but not detail , just understood it like outline so is this just normal or one needs to understand full script with detail understanding to learn scripting . and do i need to write whole script again without refering anything on my own in order to learn ?? or just ignore it and understand what one can and moving to next tutorial and i can auctomactically learn it ???
     
  10. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    It depends on what level you want to be at.

    You can get a guitar, tune it using an auto-tuner, and follow along with tabs to learn to play Stairway to Heaven. You'll then be proficient at playing Stairway without really being what I'd call a guitarist, much less a musician.

    Alternatively, you can get a guitar and learn how to tune it by ear. Learn the different notes, how to play chords and progressions, study music theory, learn to read sheet music, teach yourself to identify notes when you hear them, etc... This way takes much longer, is more effort intensive, and can be frustrating when you don't get it. But a person who sticks it out is that much better for it, and has a proficiency the first person doesn't. This person is a musician.

    If you want to be able to slap together the occasional script, you just have to know how to Google the right thing to get the results you're looking for, how to piece those things together, and how to fix errors as they pop up. Doing so, you'll be able to hack together a passable mimicry of Minecraft. But in my mind, this doesn't make you a programmer.

    If you want to be a programmer, you have to learn to program the hard way. There aren't any shortcuts. You have to read, practice, study, fail, improve, experiment, and reason. You have to pay attention to details, something I see you skimping on a lot with your posts. It's okay to ask questions, but you shouldn't have to ask the same question multiple times, because doing so indicates you didn't learn from the first question's answers. You can ask for guidance, but you need to be able to find things on your own, too.

    Anyone can do it. But you have to put in the effort.
     
    Martin_H, badassgamer and Ryiah like this.
  11. Farelle

    Farelle

    Joined:
    Feb 20, 2015
    Posts:
    504
    little tip on how to start learning things from tutorials you don't understand:
    whenever you encounter a word you don't understand or don't know the exact meaning of it or whenever anything similar happens: isolate it and research it.
    As example, you watch a tutorial which includes "for loops", but you don't understand exactly how they work, then you go and search for "for loops" tutorials/documentation etc. you do that as long as it's necessary with every little piece you don't understand and you will see that after a while, when you come back to that big tutorial you will understand more and more.
    It's important that you understand this way of learning, because you will probably have to do it again and again and again, no matter how good you are or become. Also, it does not mean that you have to memorize (thats whats happening in school usually) even though you should learn some "vocabulary" for programming, you don't have to know every tiny function that unity has to offer as example...sure it's helpful, but it's unrealistic to know everything from your head.
     
  12. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Did Farelle's approach when I started Unity. Didn't understand a word? research it. Still do, although I've run out of things I haven't understood in the last few years!
     
    Martin_H, badassgamer and Farelle like this.
  13. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,037
    Time to get deeply into quantum physics?

    I recommend starting at the source: Read basic tutorials on MSDN. Here are a couple of very broad overviews writing good, readable code, with some literature suggestion at the end:
    https://msdn.microsoft.com/en-gb/library/ff926074.aspx
    https://msdn.microsoft.com/en-us/library/aa260844(v=vs.60).aspx
    (Their first book recommendation is Code Complete, which I have read and agree with.)
     
    badassgamer and Ryiah like this.
  14. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    I did as you guys said google all words that i dont understand . i dont find any meaning for two varibles Float and int .. i just want to know what they are?? and what do they do??
     
  15. Farelle

    Farelle

    Joined:
    Feb 20, 2015
    Posts:
    504
    badassgamer and Schneider21 like this.
  16. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Yeah, man, you may want to back up a bit and focus first on learning how to Google. If you're trying to find out about a construct of the language, type "C# _SEARCH_WORD_". If it's specific to Unity, type "Unity _SEARCH_WORD_"

    Also, variables and data types are covered in the second Scripting tutorial, and are pretty much the first thing covered in ANY code book or tutorial. This suggests you either haven't actually gone through the paces of doing the learning material, or you're just following along with it and not actually absorbing what it's trying to teach you.

    When you're following along with a guide of any kind, don't just complete the steps and move on. Think about what you're doing and why. Listen to reasons provided by the instructor. Repeat the lesson if you don't feel confident you fully possess the knowledge it was imparting.

    I don't want to be rude, but if you're not demonstrating that you're putting in the requisite effort yourself, you're going to find people less willing to help you out.