• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Dok101 · Jul 19, 2015 at 11:06 AM · namespace

How to use getcomponent with a namespace?

Hi

Im currently making a multiplayer racing game and I need to use the getcomponent code in my script. I am trying to get this script from Unity's car in the Vehicles package that can be downloaded in the standard assets. However, the two car scripts im trying to get, CarController and CarUser control both have namespaces which are namespace UnityStandardAssets.Vehicles.Car

I have never come accross namespaces before so I dont really know how to use them. I looked up the documentation and created this code

 using UnityEngine;
 using System.Collections;
 namespace StandardAssets.Vehicles.Car
 
 public class NetworkPlayer : MonoBehaviour {
 
     public GameObject Mycamera; //Mycamera to follow cars
 
     void Start () {
         Mycamera.SetActive(false); //Dont set active
         GetComponent<CarController>().enabled = true; // enable them when multiplayer starts
         GetComponent<CarUserControl>().enabled = true;
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }

However this code gives me the errors :

Assets/Network/NetworkPlayer.cs(5,6): error CS1003: Syntax error, `.' expected

Assets/Network/NetworkPlayer.cs(5,44): error CS8032: Internal compiler error during parsing, Run with -v for details

Any help would be appreciated as I'm really stuck here.

Thanks in Advance

Dok

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Positive7 · Jul 19, 2015 at 11:40 AM 0
Share
 using UnityEngine;
 using System.Collections;
 namespace UnityStandardAssets.Vehicles.Car{
 [RequireComponent(typeof (CarController))]
 [RequireComponent(typeof (CarUserControl))]
 public class NetworkPlayer: $$anonymous$$onoBehaviour {
     
     public GameObject $$anonymous$$ycamera; //$$anonymous$$ycamera to follow cars
     
     void Start () {
         $$anonymous$$ycamera.SetActive(false); //Dont set active
         GetComponent<CarController>().enabled = true; // enable them when multiplayer starts
         GetComponent<CarUserControl>().enabled = true;
     }
     
     // Update is called once per frame
     void Update () {
         
     }
     }
 }

or simply

 using UnityStandardAssets.Vehicles.Car;

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by fafase · Jul 19, 2015 at 12:04 PM

A namespace is just a closed location where classes are stored. using namespaces allows the developer to make some classes/variables/methods as internal. This way, they are accessible all over the namespace like public but hidden from outside the namespace.

In your case, you would get the component just as usual. Think of it like when you use the UnityEngine.UI.

Either you add on top :

  using MyNamespace;

this means the compiler will link the namespace to that script and allows the user to access all public items. Consider you MyComponent inside MyNamespace:

 using MyNamespace;
 public class MyClass:MonoBehaviour{
      void Start(){
         var comp = GetComponent<MyComponent>();
      }
 }

The other way is to explicitly address the component:

 public class MyClass:MonoBehaviour{
      void Start(){
         var comp = GetComponent<MyNamespace.MyComponent>();
      }
 }

this may be compulsory if two different namespaces contain a MyComponent class and the compiler is not able to define which you meant.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dok101 · Jul 19, 2015 at 04:47 PM 0
Share

Thanks, I just added the using and it compiled without an error. I was really stuck so thanks!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Visual Basic Error 1 Answer

the type or namespace name Note could not be found. are you missing a using directive or an assembly reference. 0 Answers

Missing namespaces with A* Pathfinder Pro 2 Answers

Trying to access the Slider component 1 Answer

Error CS0246: Type/Namespace Error 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges