Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Crayz · Dec 02, 2014 at 05:48 AM · classnamespacepublic static

Working with namespace, correct way to set up?

I'm trying to utilize namespace though can't quite figure out how to set up my C# properly. Lets say I have a namespace named Player, with public classes deriving from MonoBehaviour - Movement & Attack.

 using UnityEngine;
 using System.Collections;
 
 namespace Player 
 {
     public class Movement : MonoBehaviour 
     {
         void Message()
         {
             Debug.Log ("Moving");
         }
     }
     public class Attack : MonoBehaviour
     {
         void Message()
         {
             Debug.Log("Attacking");
         }
     }
 }

Is this namespace solely for setting up structure? Is there a good place to define the functions and what each class does specifically? How would I go about calling these classes in another script file? This doesn't seem to work:

 using UnityEngine;
 using System.Collections;
 using Player;
 
 public class hey : MonoBehaviour {
     
     // Update is called once per frame
     void Update () {
         Player.Movement.Message();
     }
 
 }

Unless I set void Message() to public static void Message(). In what case would I not have Message() set to public static? I'll be doing some googlin' after submitting this question, any helpful/easy to understand insight would be appreciated as well :)

Comment
Add comment · Show 2
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 Habitablaba · Dec 02, 2014 at 05:52 AM 2
Share

It is generally a good idea to do your googling before posting your question, not after.

avatar image Crayz · Dec 02, 2014 at 06:15 AM 0
Share

I've actually been Googling for a while now, whenever I'm scripting or program$$anonymous$$g I'll never be found without a couple Firefox tabs in Google. I'm hoping that if I ask, maybe I'll better understand rather then trying to decipher somebody else's questions and answers

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Kiwasi · Dec 02, 2014 at 06:18 AM

There are a bunch of misunderstandings here.

First piece of advice - Don't go near namespaces until you already know what they mean and what they are used for. At your level there is no point worrying about them. Your code has to be reasonably complex before namespaces are strictly needed. And they are pretty easy to refractor in.

Same advice goes for static. Leave it alone until you already understand it. There is no case when a static is truly needed.

Your basic problem comes from a misunderstanding of access modifiers. There are four of them.

  • private - cannot be accessed outside of the class

  • protected - can be accessed by derived classes only. Forget this one for now.

  • internal - can be accessed inside the assembly. Forget this one for now. In general you won't use this unless you are building .dlls for others to use

  • public - can be accessed by everything

General rule: Make everything private, unless you really, really need to access it from another class.

As for accessing members of another class, there are thousands of question here on it. Start with googling GetComponent. Work your way up from there.

Comment
Add comment · 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
0

Answer by jaja1 · Dec 02, 2014 at 06:18 AM

If I understood what you are asking correctly: Read up on accessibility in c#.

Accessing the Message() method requires it to be public even within a public class. Declaring it as static is not required unless you want global access to the method without having to create an instance of the Movement class. This also means that any non-static variables that belong to the Movement class can not be accessed by the static Message() method because there can be several instances of the Movement class that these variables belong to.

By not declaring Message() as public you are implicitly stating that it is private and can only be accessed within the scope of the class it is declared within.

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

28 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

Related Questions

Can't access another script, think it may be due to namespace/public class ? 1 Answer

The type or namespace name 'StateMachine' could not be found (are you missing a using directive or an assembly reference?) 1 Answer

How to use MonoBehaviours inside namespaces. 3 Answers

Calling functions from other namespaces and scripts. 0 Answers

How to implement custom using directives? 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