I have been getting this error when I try to run my code for switching cameras. I don’t understand why it is telling my that the variables are null, as I have other variables assigned similarly.
“UnassignedReferenceException: The variable CutSceneCam of ‘UponCollision’ has not been assigned.
You probably need to assign the CutSceneCam variable of the UponCollision script in the inspector.”
Here is the code that I am using:
public class UponCollision : MonoBehaviour {
public Camera CutSceneCam;
public Camera MainCamera;
void Start (){
CutSceneCam.enabled = true;
MainCamera.enabled = false;
}
void OnTriggerEnter(Collider col){
if (col.gameObject.name == "CutSceneCam") {
CutSceneCam.enabled = !CutSceneCam.enabled;
MainCamera.enabled = !MainCamera.enabled;
}
}
}
Another problem has presented itself. The code now works, the animation works, but when the camera colides with the trigger box that I have, it doesn’t switch from the stationary camera to the player camera. The code is the exact same, except for the fact that the object that is supposed to be colliding with the collider has changed several times in experimentation. At the moment, I have a sphere running into a box collider, and this is supposed to switch the cameras, but it doesn’t.
This should be a comment, not an answer. At any rate, double check you didn't put the same camera on both spots, that the cameras aren't overlapping and that you didn't put the wrong script object on the wrong objects.
what I have done to rectify this issue was copy my c# script that I coded in Monodevelop
delete the C# file …
re-create the C# file - copy the script back into the C# file again
attach the variable or objects back into the script in the inspector
and the run the project again.
what I remember was that the first time around I ran the scene before I attached the objects to the script. Upon stopping the game then attaching the objects after I realized they were missing I received this error.
I figured Unity held some sort o memory that the objects were missing script so I figured I would make sure everything was in place before I hit the play button/ran the game this time around. Seems to work now. even though I get an error that the second object is now missing.
the 2nd error was due to a blank script I inadvertently attached to the 2nd object. Once I removed this empty script the warning went away.
You need to look at this object in Unity and then look to the right where you placed your script file. This area is called the inspector.
There, the script will have two spots for your two public variables - “CutSceneCam” and “MainCamera”.
You need to drag a camera object in to each to assign them.
I've done that, but for some reason, it likes to add a second script to the object that I applied the script to. Adding the objects to the second script fixed the error, however now I need to get my animations working. Thanks for the help.
How did you do it?I'm having exactly the same problem, you mean i should add another script to the Gameobject and have my variables in that script and retrieve from it into the one where it's causing problem?
This should be a comment, not an answer. At any rate, double check you didn't put the same camera on both spots, that the cameras aren't overlapping and that you didn't put the wrong script object on the wrong objects.
– Dracorat