There is no red squiggly line where Sam says there is

My namespaces in PlayerController don’t need to call any other namespaces and work fine. Is this due to the gliding player problem in the earlier lecture?

I am doing everything in order this time.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace RPG.Control

Is your Mover class within the namespace RPG.Mover, and your Fighter class within the namespace RPG.Fighter? If they have no namespace declarations, then they are in the “Global” namespace, and any script can see them regardless of namespace.

Is the reason why there are namespaces to prevent this?

In a way, yes. Namespaces have a number of benefits:

  • Keeping code organized - classes in a namespace should all share similar purpose. For example: Everything related to Combat will go in the RPG.Combat namespace. Everything related to controlling the characters goes in RPG.Control. Everything related to Stats will go in RPG.Stats
  • Highlighting circular dependencies: If you find that Control depends on Combat and Combat depends on control, you can see that there is a potential circular dependency. Circular dependencies can lead to unwanted bugs.
  • Allows you to re-use class names… Did you know that there are TWO Random options available in Unity? One is the UnityEngine.Random, which allows us to have methods like Random.Range(0,100); and the other is System.Random which allows us to have a Random object (multiple Random objects!) that generate random numbers between 0 and 1 by simply asking for the next number. They can both exist in the code base because one is in the namespace UnityEngine.Random, and the other exists in the System namespace. Using namespaces allows us to use both, depending on our need, simply by qualifying which namespace.

In very small projects, like many of the projects in the Unity 2D and 3D courses, there’s not really a great deal of need to separate your code into namespaces. In the RPG course, by the time we are done with all classes, there will be well over 100 classes in the project. Namespaces are one of the most effective tools to keep these scripts organizsed (there are 21 namespaces we will introduce in these courses, and as you add more features yourself, this will increase).

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms