none
The namespace 'global':: already contains a defination for "playercontroller" RRS feed

  • Question

  • using UnityEngine;
    using System.Collections;

    public class PlayerController : MonoBehaviour {

        public float speed;

        private Rigidbody rb;

        void Start ()
        {
            rb = GetComponent<Rigidbody>();
        }

        void FixedUpdate ()
        {
            float moveHorizontal = Input.GetAxis ("Horizontal");
            float moveVertical = Input.GetAxis ("Vertical");

            Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

            rb.AddForce (movement * speed);
        }
    }

    Monday, June 29, 2015 3:29 PM

Answers

  • Put your class in a namespace:

    using UnityEngine;
    using System.Collections;
    
    namespace Yournamespace
    {
        public class PlayerController : MonoBehaviour
        {
    
            public float speed;
    
            private Rigidbody rb;
    
            void Start()
            {
                rb = GetComponent<Rigidbody>();
            }
    
            void FixedUpdate()
            {
                float moveHorizontal = Input.GetAxis("Horizontal");
                float moveVertical = Input.GetAxis("Vertical");
    
                Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    
                rb.AddForce(movement * speed);
            }
        }
    }

    You can then use it in another class by importing this namespace:

    using Yournamespace;

    ...or by using the fully qualified name:

    var obj = new Yournamespace.PlayerController();
    You cannot define more than one class with the same name in the same namespace.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.


    • Edited by Magnus (MM8)MVP Monday, June 29, 2015 3:40 PM
    • Proposed as answer by Kristin Xie Tuesday, June 30, 2015 6:12 AM
    • Marked as answer by Pritham Tuesday, June 30, 2015 10:30 AM
    Monday, June 29, 2015 3:40 PM
  • You probably have a ‘namespace PlayerController’ in some other files. It is not permitted to have a namespace and a homonymous class, defined outside of any namespace.

    It should work if you rename your class to MyPlayerControl, for example. However it seems unusual to define such global classes without namespaces.



    • Edited by Viorel_MVP Monday, June 29, 2015 6:15 PM
    • Marked as answer by Pritham Tuesday, June 30, 2015 10:30 AM
    Monday, June 29, 2015 6:09 PM

All replies

  • Put your class in a namespace:

    using UnityEngine;
    using System.Collections;
    
    namespace Yournamespace
    {
        public class PlayerController : MonoBehaviour
        {
    
            public float speed;
    
            private Rigidbody rb;
    
            void Start()
            {
                rb = GetComponent<Rigidbody>();
            }
    
            void FixedUpdate()
            {
                float moveHorizontal = Input.GetAxis("Horizontal");
                float moveVertical = Input.GetAxis("Vertical");
    
                Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    
                rb.AddForce(movement * speed);
            }
        }
    }

    You can then use it in another class by importing this namespace:

    using Yournamespace;

    ...or by using the fully qualified name:

    var obj = new Yournamespace.PlayerController();
    You cannot define more than one class with the same name in the same namespace.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.


    • Edited by Magnus (MM8)MVP Monday, June 29, 2015 3:40 PM
    • Proposed as answer by Kristin Xie Tuesday, June 30, 2015 6:12 AM
    • Marked as answer by Pritham Tuesday, June 30, 2015 10:30 AM
    Monday, June 29, 2015 3:40 PM
  • What should I enter in namespace

    Monday, June 29, 2015 4:57 PM
  • You probably have a ‘namespace PlayerController’ in some other files. It is not permitted to have a namespace and a homonymous class, defined outside of any namespace.

    It should work if you rename your class to MyPlayerControl, for example. However it seems unusual to define such global classes without namespaces.



    • Edited by Viorel_MVP Monday, June 29, 2015 6:15 PM
    • Marked as answer by Pritham Tuesday, June 30, 2015 10:30 AM
    Monday, June 29, 2015 6:09 PM
  • What should I enter in namespace


    The namespace keyword is used to declare a scope that contains a set of related objects. You can name it whatever you want. Just surround the class with a namespace as I showed you in the previous post. Please refer to the following page for more information about namespaces in C#: https://msdn.microsoft.com/en-us/library/z2kcy19k.aspx

    Please also remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Monday, June 29, 2015 8:40 PM
  • But now I'm having another problem..
    Referenced Script on this behaviour is missing!
    Tuesday, June 30, 2015 1:54 PM
  • But now I'm having another problem..
    Referenced Script on this behaviour is missing!
    You may be better moving this question to the Unity forums.
    Tuesday, June 30, 2015 2:05 PM