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. Dismiss Notice

WTF...

Discussion in 'General Discussion' started by RageByte, Feb 2, 2020.

  1. RageByte

    RageByte

    Joined:
    Jul 2, 2017
    Posts:
    33
    Don't use CharacterController.cs or any built-in Unity class as your filename!
     

    Attached Files:

    • help.jpg
      help.jpg
      File size:
      415.8 KB
      Views:
      433
    Last edited: Feb 2, 2020
    tigerleapgorge likes this.
  2. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    9,745
    You're not accessing the engine's CharacterController class, you're accessing CharacterController.cs. You've created a conflict, so you can't actually use Unity's character controller.

    Also:
    1. Use the code tags and tell us the error, don't use a screenshot
    2. General Discussion is not a support forum
     
    tigerleapgorge and Ryiah like this.
  3. RageByte

    RageByte

    Joined:
    Jul 2, 2017
    Posts:
    33
    The weird thing is, sometime's it works and sometimes it doesn't. So how do you "access" as you say, the CharacterController class?
     
  4. RageByte

    RageByte

    Joined:
    Jul 2, 2017
    Posts:
    33
    This example is directly from the docs so. Still shrugging my head at this...
     
  5. RageByte

    RageByte

    Joined:
    Jul 2, 2017
    Posts:
    33
    Murgilod, the correct answer was to say that you used "CharacterController.cs" as your filename which conflicted with the Unity class. Please be more specific in the future.
     
  6. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    9,745
    Don't tell me how to do my job, nerd.
     
  7. RageByte

    RageByte

    Joined:
    Jul 2, 2017
    Posts:
    33
    Just did. /shrug But you did get me to thinking that the filename itself was the issue. So partial? kudos to you. Fellow nerd.
     
    tigerleapgorge likes this.
  8. Ostwind

    Ostwind

    Joined:
    Mar 22, 2011
    Posts:
    2,804
  9. RageByte

    RageByte

    Joined:
    Jul 2, 2017
    Posts:
    33
    Yeah yeah yeah move on...nothing to see here...
     
    tigerleapgorge likes this.
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I've gotten into the habit of putting all of my own code into a namespace on any serious project. Otherwise I'm almost guaranteed to collide with something eventually. You can even modify your script template to include on by default.
     
  11. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,124
    She gave the correct answer. You created a class named "CharacterController", you then created a reference to that class within the class itself, and then you tried to access it thinking you were accessing a class belonging to the engine.

    There are at least two ways to solve this. First, specify the namespace that contains the class you meant.
    Code (csharp):
    1. UnityEngine.CharacterController characterController;
    Second, choose a different name for your class.
    Code (csharp):
    1. public class MyCharacterController : MonoBehaviour {
    2.     // STUFF
    3. }
     
    Last edited: Feb 2, 2020
    tigerleapgorge and Teila like this.
  12. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,321
    You learn namespaces. CharacterController is likely within UnityEngine namespace.
     
    tigerleapgorge, Kiwasi and Ryiah like this.