I’m pretty new to Unity and am clearly out of my depth, but hoping for help. (I feel a bit like a person who’s put a lot of effort into learning how to swim, but who doesn’t know that you can only swim in a pool, if it has water in it.
)
I have several 1st person cameras in a scene each of which must remain in a fixed position (except for rotation). I have all of the cameras parented to a prefab named CameraController, which I plan to use in every scene and which is supposed to select the camera for that scene when the scene is first entered.
This is the InitialCamera script -
var activeCamera : Camera;
function Awake () {
activeCamera.enabled = true;
}
Each camera in the scene has a script named ActivateSwap_script attached to it. The ActivateSwap_script is supposed to allow the player to left click on an object named cameraMover, which activates a script on that particular cameraMover, swapping cameras and thus the player’s viewpoint. Each cameraMover is an instance of a prefab, which includes a Box Collider with a Rigidbody added and a script called SwapCameras_script. Since there can be several instances of a cameraMover prefab within range of any given camera, I need a way to specify which cameraMover needs the SwapCameras_script to be activated.
This is my ActivateSwap_script -
var rayLength = 26;
function Update() {
var hit: RaycastHit;
if(Input.GetMouseButtonUp(0)) {
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit, rayLength)) {
var cameraMover = hit.collider.transform;
//if the target collider object is hit, enable the SwapCameraScript on that target
gameObject.Find(cameraMover).GetComponent(SwapCameras_Script).SwapCameras();
}
else {
}
}
}
This is my SwapCameras_script, which (I hope) allows the correct cameras to be specified per cameraMover and to swap the cameras when the SwapCamera function is called. -
var firstCamera : Camera;
var secondCamera : Camera;
function SwapCameras() {
audio.Play();
firstCamera.enabled = false;
secondCamera.enabled = true;
}
Here are my problems -
-
My InitialCamera script does not move the player’s viewpoint to the specified camera when the scene is entered. It does allow me to specify an activeCamera though.
-
I’m getting a “!NullReferenceException: Object reference not set to an instance of object” error and I’m guessing that I’ve botched the ActivateSwap_script as well and that I’m not correctly passing the hit to the cameraMover. When a cameraMover is clicked, the swapping of cameras does not happen.
Thanks for any help.
I did manage to fix my InitialCamera script by doing this -
var activeCamera : Camera;
function Awake () {
}
function Start () {
activeCamera.enabled = true;
}
I threw the towel in and combined the three scripts into one. In the long run, I think it will be more work in assembling the game, but at least it seems to work this way.
I spoke too soon about having solved this and I really do need some direction, please.
For some reason or other the camera3/target4 combo and the camera4/target5 combo toggle, even though each of these combinations are set to enable camera1.
All of the targets and cameras are children of a CameraController that contains the script below. In the inspector, I’ve dragged the targets and cameras into the variable slots according to their level in the hierarchy. All of the cameras and targets are properly labeled and the true/false staements are correct for what should be being swapped. All of the cameras are tagged “MainCamera”.
I don’t think that it can be a problem with the rays going too far, as I’ve set the variable for rayLength to 22 and targets 4 and 5 are more than 150 apart. I’ve also got the camera clipping planes set to 42, just enough to “see” what they need to see. I’ve experimented with camera depth, distance, clipping plane, and still my code is wonky.
Can anyone help, please?
var target1: Transform;
var target2: Transform;
var target3: Transform;
var target4: Transform;
var target5: Transform;
var target6: Transform;
var camera1 : Camera;
var camera2 : Camera;
var camera3 : Camera;
var camera4 : Camera;
var rayLength = 22;
function Awake() {
}
function Start() {
camera1.camera.enabled = true;
camera2.camera.enabled = false;
camera3.camera.enabled = false;
camera4.camera.enabled = false;
}
function Update () {
if (Input.GetMouseButton(0)) {
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
if (Physics.Raycast(ray, hit, rayLength)) {
if (hit.transform == target1) {
camera1.camera.enabled = false;
camera2.camera.enabled = true;
camera3.camera.enabled = false;
camera4.camera.enabled = false;
print ("target1 hit!");
} else if (hit.transform == target2) {
camera1.camera.enabled = false;
camera2.camera.enabled = false;
camera3.camera.enabled = true;
camera4.camera.enabled = false;
print ("target2 hit!");
} else if (hit.transform == target3) {
camera1.camera.enabled = false;
camera2.camera.enabled = false;
camera3.camera.enabled = false;
camera4.camera.enabled = true;
target3.audio.Play();
print ("target3 hit!");
} else if (hit.transform == target4) {
camera1.camera.enabled = true;
camera2.camera.enabled = false;
camera3.camera.enabled = false;
camera4.camera.enabled = false;
print ("target4 hit!");
} else if (hit.transform == target5) {
camera1.camera.enabled = true;
camera2.camera.enabled = false;
camera3.camera.enabled = false;
camera4.camera.enabled = false;
target5.audio.Play();
print ("target5 hit!");
} else if (hit.transform == target6) {
camera1.camera.enabled = true;
camera2.camera.enabled = false;
camera3.camera.enabled = false;
camera4.camera.enabled = false;
print ("target6 hit!");
}
} else {
}
}
}
Unless you’re rendering the views from all the cameras at the same time you’re going about this the wrong way.
What you should do is set up your single “MAIN” camera, and have a list of Vector3 points which define your other locations.
When you want to swap to a different point, all you do is something like the following:
Vector3[] CamPos = new Vector3[4]; //as an example!
Vector3[] CamTarg = new Vector3[4]; // for the targets
Camera.main.transform.position = CamPos[x];
Camera.main.transform.LookAt ( CamTarg[x] );
…or something similar to that. You use the arrays of Vector3’s to define your camera’s positions and the location they want to look at, and you can just swap them out as you need to.
This way, you don’t need to bother about multiple cameras. Most people just use multiple cameras to either render UI’s or special effects - you don’t use them for storing fixed camera positions.

Thanks so much, S7ARBVCK, for your reply. I’ll go back and try snapping the camera from place to place as you suggest. It does seem to be a more logical approach. I’m trying to create a Riven-style node based game, so the camera needs to move from the center of my cubic panos, each of which represents a node in the game.
I’ll still need the raycasting and target objects though (I think), because they are acting as my “hotspots” for movement and getting to the close-ups and puzzle screens. I suspect that’s where most of my mistakes lie.
I think that I can do some this with Brady’s EZ utilities, but I was trying to make myself do it without his tools first as a learning tool.
Thanks again.