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 drewmanfu · Oct 25, 2011 at 12:33 AM · cameragameobjectprefab

Setting a variable : camera by using findObjectWithTag

So the problem I'm having is I have camera set up on the prefab of my turret. This prefab is then used to randomly spawn on a map. Now because its randomly spawned on the map I cannot attach cameras to the object directly. Well I can attach the camera on the prefab since it will never change what I want to do is include the main camera in this list of camera but I can't put it on the prefab. The turret is controlled by the player and I want him to be able to switch between the main view and turret view.

So what I'm trying to do is set my 2nd camera to an object tagged Main Camera. I'm not sure how to do this and have been looking all over the place for an answer.

here is my code that I have so far

 var camera1 : Camera;
 
 var camera2 : Camera;
 
 private var t = 1;
 
 function Update(){
 
 
     if(t == 1)
 
     {
 
         camera1  = myObject.camera(GameObject.FindGameObjectWithTag("MainCamera"));
 
         camera1.enabled = true;
 
         camera2.enabled = false;
 
          t = 2;
 
     }
 
     if(Input.GetButtonDown("1")){
 
 
         if(camera1.enabled == true){
 
             camera1.enabled = false;
 
             camera2.enabled = true;
 
         }
 
         else{
 
             camera1.enabled = true;
 
             camera2.enabled = false;
 
         }
 
     }
 
 }


this code does not work since GameObjects can not be changed to UnityEngine.Camera is there a way to do this by getting the camera and storing it into a variable.

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
Best Answer

Answer by syclamoth · Oct 25, 2011 at 12:38 AM

Instead of putting cameras on the turrets, why don't you just have empty transforms where the camera should go? Then, when you want to switch camera views, do something like this-

 Camera.main.transform.parent = currentTurretTrans;
 Camera.main.transform.localPosition = Vector3.zero;
 Camera.main.transform.localRotation = Quaternion.identity;

Remember, if you're using this method, you need to put all of your various camera controller scripts on empty transforms, and then when you want to switch cameras, call that bit of code on the transform that you want to switch to.

Comment
Add comment · Show 1 · 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 drewmanfu · Oct 26, 2011 at 01:47 AM 0
Share

thank you :).

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Acessing scripts in other game objects with prefabs, without using GameObject.Find 1 Answer

How do I assign a prefab reference to a private variable? 1 Answer

Reinitialize prefab 0 Answers

Creating entities with some variations among them 0 Answers

GUI canvas' WorldSpace camera not copied into prefab 1 Answer


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