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

I'm creating a character selection scene where their are two player. But i'm getting this error

Discussion in 'Scripting' started by Recluse-Rohan, Mar 7, 2020.

  1. Recluse-Rohan

    Recluse-Rohan

    Joined:
    Dec 14, 2015
    Posts:
    26
    The empty game object is called ModelsContainer where I've put both of my characters as children and added a C# script.
    But I'm getting this error
    (Assets\Playerselectonscript.cs(7,9): error CS0246: The type or namespace name 'game object' could not be found (are you missing a using directive or an assembly reference?))
    Please see the code, and guide me on what's wrong.
    Thanks.


    code.png
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    The error is pretty clear. In your used namespace (the scope of code you work in), 'gameobject' is not known to the compiler. This indicates that either you did forget an import, did not yet create a variable with this identifier, or misspelled the identifier. In this case it is the latter, as 'gameobject' does not exist. The type is called 'GameObject', and the quick reference to your own gameobject is called 'gameObject'. Identifiers are always case-sensitive.
    You intended to use the type called GameObject.

    Also, if i may add this: consider looking up some naming conventions for C#. Variables should be written in camelCase, while class, method and property names should be written using UpperCamelCase. Your class names are missing any upper letters except the first, while your variables are written with UpperCamelCase.
    This probably contributed to the above mistake aswell. On that note, you did the same for the method 'GetChild', which you wrote as 'getchild'. SetActive the same, just as is the case with childCount. Looking up some naming conventions and following them would also help you intuitively writing things correctly. If you knew what conventions are being used (and did this yourself), then you'd intuitively write 'GetChild', 'GameObject', 'childCount' and 'SetActive' in the right way.
     
    Last edited: Mar 7, 2020
  3. Recluse-Rohan

    Recluse-Rohan

    Joined:
    Dec 14, 2015
    Posts:
    26
    Please see now ?
    Untitled.png
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    It looks better, but there is still a lot wrong with it. It would help me help you if you used code tags to post code examples, instead of screenshots.

    I could imagine you still have quite a few errors:
    • If you are refering to Monobehaviour.Start() and want the contents of your start function to be executed once, then you also need to write it in the correct way: Start(), not start().
    • You write transform.ChildCount, but you mean transform.childCount, so it wont find ChildCount
    • In the for-loop you are using a variable i, but never declare it. It should be for(int i = 0; ... )
    • In line 17 you do not mean the type GameObject, but instead the reference to the childs' gameobject, which is 'gameObject'
    These are just the things directly impacting whether or not your code will execute.
    As far as naming conventions go, your CharacterList should be names characterList, since it's a normal variable.
    This all probably sounds a lot more confusing than it is, so try following this simple rule: default, you write camelCase. For class names, method names and property names, you use UpperCamelCase. There are a few more conventions, but those are the most important ones.
    It is also work mentioning that you can name things however you want. Naming conventions are just there to help you and others read your code more easily, by making it uniform to other code and by making different types of itentifiers more easy to distinquish. When working with already existing identifiers (gameObject, childCount, .. those were written by someone else than you), it is however important to write them exactly as they were defined, otherwise the compiler wont know what you mean and throw you an error.

    I believe you are very, very new to programming. This is, of course, absolutely fine. I would however suggest you start at the very beginning. There are plenty of good C# and Unity tutorials out there, but if you want my recommendation i always like to link this one:
     
  5. Recluse-Rohan

    Recluse-Rohan

    Joined:
    Dec 14, 2015
    Posts:
    26
    It means a lot if someone marks your mistakes, but I'm just starting out with this, and can't really help myself, so if you can just tell me the exact codes, in the above project, it would make it cinch for me and reduce my headache also.
    I appreciate your help.
    Thanks.
     
  6. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    If you post me the code (using code tags, look up the sticky post on this subforum if you dont know how) i can fix your typos. I'm having a hell of a headache right now tho, so i'm not really looking forward to copy it from a screenshot^^