I’ve never really had to deal with namespaces before, so excuse any ignorance that might be present here.
I’m writing a static camera system (kind of like you’d see in the old Resident Evil games) and I’m currently using Unity’s Third Person Controller. I’m trying to modify ThirdPersonUserControl.cs for the needs of this system.
Basically, I want to subscribe it to a custom event so that every time my active camera changes, ThirdPersonUserControl will update its reference to the camera appropriately (as this reference is what allows for camera relative controls).
I have a class CameraMaster which contains the event I want to subscribe to, but I can’t access it from ThirdPersonUserControl. I’m pretty sure this has something to do with namespacing, as ThirdPersonUserControl resides in the namespace UnityStandardAssets.Characters.ThirdPerson.
Every time I try to reference CameraMaster in ThirdPersonUserControl, I get the error message
The name ‘CameraMaster’ does not exist in the current context.
I’ve tried namespacing CameraMaster and putting in the appropriate using
statement in ThirdPersonUserControl, as well as putting CameraMaster in the same namespace. Nothing seems to have worked. I even tried using global::
.
I read in This Question that the problem was that the other class was in a plugins folder, however moving the scripts into the same directory also didn’t work for me.
I know that there are other solutions to this problem (storing a reference to the ThirdPersonUserControl in CameraMaster and manually calling a function to do what I want, or even just moving a single camera to different locations, rather than having multiple cameras), however, for the purposes of learning, I really want to know how to access my CameraMaster class from ThirdPersonUserControl.
It’s probably not relevant, but here is CameraMaster:
public class CameraMaster : MonoBehaviour {
[SerializeField]
private Camera startingCamera;
private static Camera[] cameras;
public delegate void CameraEventHandler(Camera cam);
public static event CameraEventHandler OnCameraChange;
void Start() {
cameras = GameObject.FindObjectsOfType<Camera>();
ShowCamera(startingCamera);
}
public static void ShowCamera(Camera cam) {
if (OnCameraChange != null) {
OnCameraChange(cam);
}
foreach (Camera camera in cameras)
camera.enabled = camera == cam;
}
}
And, for reference, here is ThirdPersonUserControl’s class definition:
using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
namespace UnityStandardAssets.Characters.ThirdPerson {
[RequireComponent(typeof(ThirdPersonCharacter))]
public class ThirdPersonUserControl : MonoBehaviour {
Thanks for any help!