Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ThunderK · Nov 23, 2013 at 09:11 PM · c#camerajavascripttriggerswitch

Camera switching : C# onTriggerEnter not working

Hello. I'm actually making a game in which there are many cameras, and the switch between them occurs when the player enters a specific trigger. In the actual level I'm working on, I have 9 different cameras.

At first, I did it with Javascript, with the following code :

 var cam1 : Camera;
 var cam2 : Camera;
  
 function Start() {
     cam1.enabled = true;
     cam2.enabled = false;
 }
  
 
 function OnTriggerEnter (player : Collider) {
 
             cam1.enabled = !cam1.enabled;
                 cam2.enabled = !cam2.enabled;
 
 }

It worked perfectly until the gestion of the cameras went wrong. It either chose the wrong one to start, or failed to switch to a camera, even though it worked before. I assumed it was due to Javascript's instability.I then decided to switch to C# and used this code :

     public Camera cam1;
     public Camera cam2;
     
     // Use this for initialization
     void Start () {
         
         cam1.enabled = true;
         cam2.enabled = false;
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     
     void onTriggerEnter (Collider other) {
         
         if(other.gameObject.tag.Equals("Player")) {
         
          cam1.enabled = !cam1.enabled;
                 cam2.enabled = !cam2.enabled;
             Debug.Log("pass");
             
         }
         
         
     }
 }

Which fails. The trigger detection doesn't work at all, since the Debug.Log doesn't. My player has a capsule Collider and a a Rigidbody component. The trigger box has a collider set to "Trigger". I'm kind of lost since I really don't know what to do anymore. I searched through answer.unity3d for hours now but can't find a proper solution.

Thanks for your help !

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Roland1234 · Nov 26, 2013 at 07:02 PM

Can't speak to the Javascript code, but I can tell you that in the C# version your method should be OnTriggerEnter with a capital O. Unity won't call it spelled otherwise.

In the future I'd advise you to add your Debug.Log calls exactly where you want them - currently you couldn't properly say that the trigger method wasn't even being invoked since it might very well have been, but your other GameObject might not have been tagged "Player" or some operation you were performing before outputting the log might have caused an exception and prevented its execution, etc.

Regarding your specific issue: Well - like I kinda mentioned, make sure that your other object is actually tagged as "Player" and that its Collider and Rigidbody components are actually active. Verify the object with the Collider trigger is also active and that your camera references are setup correctly in the inspector as well. If that doesn't help you might need to include more details of your scene and setup.

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Switching Cameras - Confusion with Two Code Samples 2 Answers

Switching cameras (JavaScript) 0 Answers

Check for collision while animating 0 Answers

Multiple Cars not working 1 Answer

switching cameras C# 4 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