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 derrtyones · Dec 14, 2012 at 01:37 PM · camerajavascriptnullreferenceexception

Switching camera's error

Hi,

I have a first person controller with a "Main Camera" and I have a second camera somewhere else on the scene named "Puzzle Camera".

When I hit play, I immediately see from the "Puzzle Camera" view, but I should start at the Main Camera instead. To do this, I use this code:

 function Start (){
     GameObject.Find("Puzzle Camera").active = false;
 }

When I use that script I no longer start seeing from that camera. Now, I also placed a box somewhere which works with an OnTriggerEnter function. Once I hit that box, I want to view from the Puzzle Camera and not the Main Camera and once the puzzle is solved, I want to switch back to the Main Camera.

I used this:

 function OnTriggerEnter (player : Collider) {
     if (player.gameObject.tag == "Player") {
         if(triesLeft == true) {
             
             //Switch to puzzle camera
             GameObject.Find("Puzzle Camera").GetComponent("Camera").active = true;
             GameObject.Find("Main Camera").GetComponent("Camera").active = false;
 
         }
     }
 }

Once I hit the box I get a "NullReferenceException" error. How do I solve this? I am using Unity 4.

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

2 Replies

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

Answer by Nidre · Dec 14, 2012 at 02:02 PM

Find method cannot find disabled objects.Your GameObject.Find("Puzzle Camera").GetComponent("Camera").active = true; line returns null becasue of that.

You can save a referance to the Puzzle Camera before deactivating it on the start Method. Or you can find your camera by using Camera.allCameras[indexOfYourCamera]; (Cameras are usually ordered by their depth value)

Comment
Add comment · Show 4 · 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
avatar image derrtyones · Dec 14, 2012 at 02:10 PM 0
Share

Thank you for the info! Is there also a way for me to disable/enable a camera by name without the find method? I used: "Camera.allCameras[10].active = false;" (the cam depth is set to 10) and I get an error: IndexOutOfRangeException: Array index is out of range).

avatar image Nidre · Dec 14, 2012 at 02:12 PM 0
Share

As i said you can use Camera.allCameras[indexOfYourCamera] to acces all the cameras in your scenes wheter they are active or not.

No problem at all.(a little up vote or accepting my answer would be nice for some karma tho:))

avatar image derrtyones · Dec 14, 2012 at 02:14 PM 0
Share

EDIT: thanks, I understand it now! :-)

avatar image Nidre · Dec 14, 2012 at 02:20 PM 0
Share

Ah you got me wrong.

allCameras is an array. So if you have 2 Cameras,Lets say Camera1 is depth 10 and Camera2 have depth 5 the array should cointain the following.

{Camera2,Camera1}

And if you wanted call the Camera2 you should have wrote "Camera.allCameras[0]" and for Camera1 this "Camera.allCameras[1]"

Btw im not certainly sure about this order works by depth value.It's just something i have experinced a couple times.

avatar image
0

Answer by napen123 · Dec 15, 2012 at 06:40 AM

Have you tried camera.active?

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

11 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

Related Questions

NullRefenceException error 1 Answer

I need help with camera movement and rotation. 1 Answer

How can I set a following camera which doesn't rotate with the object 1 Answer

NullReferenceException: Object reference not set to an instance of an object (when doing js to add value to c#) 1 Answer

Null Reference Exception - Object Not Set 0 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