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 :slight_smile:

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.

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.