- Home /
Active/Desactive Cameras
Hi,
I have some problems with active or desactive cameras, I have coded a Game Manager for my game, I mean, on my main menu I have two options: Single Player and Cooperative, on the menu I pass an object called gameman with the info about the number of players. And well on the game I have two cameras for the Single Player Game (one near and one far) and 4 camera for cooperative (2 near and 2 far). By default I have enabled cooperative cameras (and listener it's on near of player1 on cooperative). I have done the following script to remove not usefull cameras, but when I try to use it, my game or unity crashes. What I'm doing wrong?
here you have the variables I have used:
var Player1 : Transform;
var Player2 : Transform;
//Camara
var camaraPersonajeSingle: GameObject;
var camaraPersonajeSingleFar: GameObject;
var camaraPersonajeDoubles1: GameObject;
var camaraPersonajeDoubles2: GameObject;
var camaraPersonajeDoubles1Far: GameObject;
var camaraPersonajeDoubles2Far: GameObject;
Here you have the code:
gameman = GameObject.Find("/TypeOfGameManager").GetComponent(TypeOfGameManager);
//No hay GameManager
if (!gameman) Debug.Log("No hay GameManager");
if (gameman.numeroDePlayers==1){
Player2.GetComponent(ThirdPersonStatus).SendMessage("DestruccionDeObjeto");
camaraPersonajeDoubles1.GetComponent(AudioListener).enabled = false;
//camaraPersonajeDoubles1.active = false;
//camaraPersonajeDoubles2.active = false;
//camaraPersonajeDoubles1Far.active = false;
//camaraPersonajeDoubles2Far.active = false;
//Activamos camaras 1 jugadores
camaraPersonajeSingle.active = true;
camaraPersonajeSingle.GetComponent(AudioListener).enabled = true;
camaraPersonajeSingleFar.active = true;
}
if (gameman.numeroDePlayers==2){
camaraPersonajeSingle.active = false;
camaraPersonajeSingleFar.active = false;
}
If instead of taking GameObject type I take Camera type for the variables Unity crashes instantiantly, If not I get this error
PPtr cast failed when dereferencing! Casting from Camera to GameObject! UnityEngine.GameObject:set_active(Boolean)
On this Line camaraPersonajeDoubles1.active = false;
Also ins$$anonymous$$d of having all these cameras would it not be better just to move them to where you want them?
Answer by Statement · May 29, 2011 at 11:14 AM
The error message sounds like camaraPersonajeDoubles1 is actually referencing a Camera instead of a GameObject. Why this is happening, I can't really tell, and there is no active line of code in your snippet that actually run the line you mention break (they are commented out).
A far shot would be if there is any error in Unity Editor where your public variables at one point were Cameras, you bound them, and then changed the type to GameObject without renaming the variables. Maybe the reference still stuck as pointing to a camera but they are now being referenced as a game object type. That would theoretically cause a casting error, though I have a hard time wrapping my head around how that would even be possible.
Did you previously have:
//Camara
var camaraPersonajeSingle: Camera; // Note the type is Camera!
var camaraPersonajeSingleFar: Camera;
var camaraPersonajeDoubles1: Camera;
var camaraPersonajeDoubles2: Camera;
var camaraPersonajeDoubles1Far: Camera;
var camaraPersonajeDoubles2Far: Camera;
And later changed it to this?
//Camara
var camaraPersonajeSingle: GameObject; // Note the type is GameObject!
var camaraPersonajeSingleFar: GameObject;
var camaraPersonajeDoubles1: GameObject;
var camaraPersonajeDoubles2: GameObject;
var camaraPersonajeDoubles1Far: GameObject;
var camaraPersonajeDoubles2Far: GameObject;
If you did, set all the references to null, and re-assign them. Again, this is a far shot and I wanted to see if this was the case because of the error message description. If it solves the problem then it seems Unity has a problem with changing types of a script where the references are kept when they should be cleared.
Your answer
Follow this Question
Related Questions
No Glitch Please ... 1 Answer
What can cause Unity to crash? 3 Answers
Unity stops working when I play the game in editor 2 Answers
Clamping a Manipulated Camera 0 Answers
Camera's wont change 2 Answers