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
1
Question by jcml_ · Nov 07, 2013 at 01:10 AM · gameobjectphotongetcomponentenable

How do I enable a camera from another GameObject?

Hello, I have a script that is trying to enable a child camera on a seperate object. For some reason I can not figure out how to do this successfully. I have tried a few different methods. E.G. Player.GetComponentInChildren().enabled = true;

But this returns an error Assets/Scripts/RandomMatchmaker.cs(39,47): error CS0246: The type or namespace name 'camera' could not be found. Are you missing a using directive or an assembly reference?

I guess what I am looking for is some sort of example in C#. If it helps I am attempting to make a photon networked game, and I need this code to Instantiate a player and activate it's camera.

Thanks, if there is anything else you need to know feel free to ask in the comments.

Comment
Add comment · Show 1
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 tw1st3d · Nov 07, 2013 at 01:10 AM 0
Share

Please do not spam-post the same question like that.

1 Reply

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

Answer by Crazydadz · Nov 07, 2013 at 01:33 AM

Just the basic idea:

 GameObject player = Instantiate(PlayerGameObject); // Instantiate the player
 
 Camera cameraInChildren = player.GetComponentInChildren<Camera>(); //Find the first Camera component in his children
 
 cameraInChildren.enabled = true;
 
Comment
Add comment · Show 3 · 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 jcml_ · Nov 08, 2013 at 12:32 AM 0
Share

Wow, Thanks I used your code with some modifications and it worked perfectly!

avatar image JerryC92 · Sep 24, 2017 at 06:12 AM 0
Share

Is there any way to find any other cameras that may also be attached to the GameObject? This answer works well for finding the first camera, but I'm unable to use it for other camera components.

avatar image Mihaere · Sep 24, 2017 at 10:28 AM 1
Share

@JerryC92 First of all, this is a really old thread. But to answer your question you could use GetComponentsInChildren<>(). The "s" is crucial.

The difference is GetComponentsInChildren returns an array of any specific type it can find in the parent objects children. This means if you want to reference a particular type in the array you'll have to use an index.

 Camera[] camerasInChildren;
 
 void Start (){
     camerasInChildren = GetComponentsInChildren<Camera>();
 }


And to reference a Camera I'd use this:

 int useCameraIndex = 1;
 Camera usedCamera;
 Camera[] camerasInChildren;
 
 void Start () {
     usedCamera = cameraInChildren [useCameraIndex];
 }

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

19 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 avatar image avatar image

Related Questions

Enable My Script on Spawn 0 Answers

enable and disable boxcollider (whats wrong with my script?) 2 Answers

Creating a single-line function for GameObject.Find and GetComponent (for multiple components) 3 Answers

Get components of multiple instantiated objects? 2 Answers

Calling setActive(false) on a disabled object? 3 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