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 mrcyangaming · Feb 16, 2018 at 09:06 PM · unityeditor

How do I load a Camera into a prefab that isn't in the scene?

When inspecting the prefab object (that isnt in the scene), I'm trying to add a Main Camera to the Script component, but there is no option to add it.

I know you can add the camera when your object is in the scene, but how do I add it when the object isn't in the scene? Is it possible to do in the script?

Thanks

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
0

Answer by AhmadSannan · Feb 16, 2018 at 09:29 PM

You can use the find function.

camera = GameObject.find("CameraNameGoesHere");

Comment
Add comment · Show 5 · 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 mrcyangaming · Feb 16, 2018 at 09:32 PM 0
Share

I tried: public Camera mainCam = GameObject.Find("$$anonymous$$ain Camera"); but it gives me the following error: "Cannot implicitly convert type 'UnityEngine.GameObject' to UnityEngine.Camera"

avatar image Meguia mrcyangaming · Feb 17, 2018 at 11:10 AM 0
Share

Cast the Find result as UnityEngine.Camera.

But I still don't understand what you want to achive. $$anonymous$$ainCamera means that it is a simple camera in the scene with a special Tag. Also, GameObject.Find returns a reference o a GameObject into the scene.

You can read more about $$anonymous$$ainCamera here and about GameObject.Find here

avatar image pako Meguia · Feb 17, 2018 at 11:51 AM 0
Share

Casting will not work, i.e. cannot cast a GameObject to a Camera. He has to use GetComponent<Camera>():

 Camera mainCam = GameObject.Find("$$anonymous$$ain Camera").GetComponent<Camera>().
 


avatar image pako mrcyangaming · Feb 17, 2018 at 11:47 AM 0
Share

In your statement Camera mainCam = GameObject.Find("$$anonymous$$ain Camera"); you do 2 things"

  1. You declare the variable mainCam to be of type Camera

  2. You assign the result of the GameObject.Find method to the mainCam variable

However, the GameObject.Find method returns a GameObject. So, you are trying to assign a GameObject to a variable that can only accept a Camera. Hence the error you are getting.

I think you will benefit to make use of the Unity Tutorials, especially the "Scripting" section.

avatar image AhmadSannan · Feb 21, 2018 at 06:53 PM 0
Share

@mrcyanga$$anonymous$$g all you need to do is that mainCam should be of type GameObject, and then if you want the camera u can use : mainCam.GetComponent() and now you have a reference to the camera component of the mainCam

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

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

Related Questions

Error "Can't load custom inspector ... because the inspected type is null." for custom editor in a dll 3 Answers

when we double click any button this is error occurring, where is the error 2 Answers

Is Unity 2017 still "pay to own"? 0 Answers

Unity VR app or separate unity android app 1 Answer

In ARKIT using unity how to place the models in only one specific direction? 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