- Home /
Error. Unity can't seem to find my camera when set as a game object.
I have two different scripts set up. One on my characters (which are set up as prefabs) and one on the main camera. I have a swipe-to-move-camera function working on my camera, but I want to disable it when the player clicks on and drags the character. So I set up a bool variable on the camera script and have the character script tell it when to turn on and off. Problem is that I keep getting an error that says:
NullReferenceException: Object reference not set to an instance of an object.
The error is from my OnMouseUp and OnMouseDown functions in my character script. So it seems that it can't find the game object I'm trying to point it to. I have several other game objects set up in exactly the same way and they are able to receive messages from the character script so I'm wondering what the issue is.
Here is the relevant code I have on the character script: var mainCam : GameObject;
function Start()
{
mainCam = GameObject.Find("MainCamera");
}
function OnMouseDown()
{
mainCam.SendMessage("LaunchMode");
}
function OnMouseUp()
{
mainCam.SendMessage("NotLaunchMode");
}
And here is the relevant code from the camera script: var launchMode : boolean;
function LaunchMode()
{
launchMode = true;
}
function NotLaunchMode()
{
launchMode = false;
}
function Update()
{
if(!launchMode)
{
//Swiping code
}
}
Thanks in advance for any help!
Please post a screenshot of your scene hierarchy. Is it definitely called "$$anonymous$$ainCamera"?
Answer by FlaSh-G · Aug 29, 2014 at 09:41 PM
The Main Camera GameObject is named "Main Camera", not "MainCamera". GameObject names are not a good thing to rely on anyway. When it comes to the Main Camera, you can access it via Camera.main .
Your answer
Follow this Question
Related Questions
Another camera drag script, can anyone figure out these errors? 1 Answer
I want to put main camera floating 0 Answers
Rotate an Object 1 Answer