• 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 /
  • Help Room /
avatar image
1
Question by adi4x · Dec 04, 2015 at 07:50 AM · namespace

How can I access a method from a namespace to a class not belonging to any namespace ?

How can I access a method from a namespace to a class not belonging to any namespace ? (or it does but I don't know what to use)

EXAMPLE:

       //CLASS 1
         using UnityEngine;
         using System.Collections;
         public class Human: MonoBehaviour
         {
         static public void Hello ()
         {}
         }
 
 


And

       //CLASS 2
             using UnityEngine;
             using System.Collections.Generic;
             using System.Linq;
             namespace Company.UI.Magic
             {
             public class SomeClass: MonoBehaviour
             {
             void Start ()
             {
             //QUESTION . How can I run Hello Method from here??? Human.Hello() is not working!
             }
             }
             }
 

NOTE: CLASS 2 WAS IN PLUGIN FOLDER - THIS CAUSED MY PROBLEMS It's solved now!

Comment
Add comment · Show 12
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 Bonfire-Boy · Dec 04, 2015 at 09:25 AM 0
Share

What do you mean by "not working"?

avatar image adi4x Bonfire-Boy · Dec 04, 2015 at 10:55 AM 0
Share

They are not working because they don't share the namespace. How can I find the namespace of class 1 ?

avatar image Rostam24 adi4x · Dec 04, 2015 at 11:02 AM 1
Share

Well if you are right, then maybe using global will help you? http://stackoverflow.com/questions/2767782/explicitly-refer-to-a-class-without-a-namespace-in-c-sharp

Show more comments
Show more comments
avatar image adi4x Bonfire-Boy · Dec 04, 2015 at 12:53 PM 0
Share

Human keyword turns red in $$anonymous$$onoDevelop editor. If I return to Unity I have the next compilation error

error CS0103: The name `Human' does not exist in the current context

avatar image Bonfire-Boy adi4x · Dec 04, 2015 at 01:06 PM 1
Share

Have you tried Rostam24's suggestion? If it is a namespace issue (as opposed, for example, to the Human script just not being in the project) then I would expect that to clear it up.

avatar image Rostam24 · Dec 04, 2015 at 09:25 AM 0
Share

Hmm, you've written 'static public void Hello'. Try 'public static void Hello' ins$$anonymous$$d and see if that works.

avatar image Bonfire-Boy Rostam24 · Dec 04, 2015 at 09:32 AM 0
Share

Any particular reason you think that might make a difference?

avatar image Rostam24 Bonfire-Boy · Dec 04, 2015 at 09:43 AM 0
Share

Well simply because I've never seen it before, and thought that maybe the access modifier had to come first. But looks like I was wrong!

avatar image adi4x Rostam24 · Dec 04, 2015 at 10:59 AM 0
Share

public static it's just a (good) choice for readability.

1 Reply

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

Answer by Bunny83 · Dec 04, 2015 at 01:49 PM

Since your Human is in the global namespace you can access it from any subnamespace without any further actions. However if you have another "Human" class inside your subnamespace or another imported namespace you have to use the "global::" prefix if you want to reference a class from the global namespace.

If you don't have another class called Human, i'm pretty sure your issue isn't related to namespaces at all. The namespace "Company.UI.Magic" sounds like some sort of framework of your company. If those classes are in the first compilation group, they get compiled to a seperate assembly than things which belong to the normal compilation group. Things in "Standard Assets" or "Plugins" are compiled together into the "first" assembly. From the normal compilation group (things which are not in Standard Assets or Plugins) can access everything from the first group as the normal group is compiled last so it can reference the first assembly.

Things in "Standard Assets" / "Plugins" can't access anything from outside those folders. Frameworks should be standalong modules which aren't dependent on other classes which don't represent a framework. If you're unsure what actually belongs to a framework, don't place those files in Standard Assets or Plugins but move them to a normal folder inside your Assets folder.

Comment
Add comment · Show 2 · 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 adi4x · Dec 05, 2015 at 09:30 AM 0
Share
  • The file it's in the Plugins indeed! It is a plugin containing over 180 files and scripts and I cannot move it.

  • I still cannot access the global namespace. Can anyone suggest me the syntax for my example?

  • As a last way to communicate, I'm thinking to use PlayerPref to communicate with my class. But I will need a listener to check the PlayerPref value and it's not a nice way to code.

  • Conclusion: By now I have to start using namespaces but I still don't know if that can halp me if the classes are in the Standard Assets or Plugins folders

avatar image adi4x · Dec 05, 2015 at 11:56 AM 0
Share

I managed to move only the scripts I wanted so now my problem it's SOLVED !!!

As @Bunny83 said, having some classes in the first compilation group (Plugins folder), restricted me to gain access to my public static methods from other location (normal compilation group).

Thank you very much!

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

34 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

When to use a namespace? 2 Answers

Monobehaviour Update not working in namespace 2 Answers

I have problem with the type name or namespace name 'bl_SettingPropiertis' could not be found. Please help me with this. 1 Answer

I have an error and don't know what the cause is, help? 1 Answer

How to commuunicate scripts form default namespace? 1 Answer


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