Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Infinity · May 26, 2011 at 11:36 PM · cameracrash

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;

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CarlLawl · May 27, 2011 at 01:56 AM 0
Share

Can you format and structure your code so its readable?

avatar image CarlLawl · May 27, 2011 at 02:01 AM 0
Share

Also ins$$anonymous$$d of having all these cameras would it not be better just to move them to where you want them?

avatar image Infinity · May 27, 2011 at 02:11 PM 0
Share

Done, hope you could help me now

avatar image SirGive · May 29, 2011 at 10:28 AM 0
Share

bumpity for an answer.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges