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 /
  • Help Room /
avatar image
1
Question by Durakken · Mar 31, 2016 at 11:11 PM · gameobjectprefabsactivatenesteddeactivate

How do I enable/disable a camera on an instantiated prefab?

What I'm doing is loading my Player Character as a prefab which then has the ability to instantiate a 2nd prefab that is supposed to be able assume control while the player character is locked in place.

The prefab instantiated by the player character and the player character has a camera (nested) attached to them. I'm trying to slect the prefab's camera so that when the player is controlling the prefab te prefab's camera is active as the main camera.

The problem I'm running into is that I can't figure out how to refer to the prefab's camera to activate or deactivate it.

"gameObject.GetComponent" only refers to the components of the prefab and not to the nested camera so I'm lost.

Here's the script as I have it now...

 public class PlayerController : MonoBehaviour {
 
     public float rotateSpeed;
     public float forwardSpeed;
     public float jumpHeight;
     public Transform Armana;
     public Camera MainCamera;
 
     private Camera ArmanaCam;
     private CharacterController playerController;
     private bool PlayerActive = true;
 
     // Use this for initialization
     void Start () {
         playerController = GetComponent<CharacterController> ();
         MainCamera.enabled = true;
     }
 
     // Update is called once per frame
     void Update () {
         if (Input.GetKeyDown (KeyCode.Q)) {
             if (PlayerActive) {
                 PlayerActive = false;
                 Armana.GetComponent<PMonsterController>().enabled = true;
                 MainCamera.enabled = false;
                 MainCamera.GetComponent <AudioListener>().enabled = false;
                 ArmanaCam.enabled = true;
                 ArmanaCam.GetComponent <AudioListener>().enabled = true;
             } else {
                 PlayerActive = true;
                 Armana.GetComponent<PMonsterController>().enabled = false;
                 ArmanaCam.enabled = false;
                 ArmanaCam.GetComponent <AudioListener>().enabled = false;
                 MainCamera.enabled = true;
                 MainCamera.GetComponent <AudioListener>().enabled = true;
             }
         }
 
         if (Input.GetKeyDown (KeyCode.E)) {
             Instantiate (Armana, transform.position, transform.rotation);
             //Assaign ArmanaCam... somehow
             ArmanaCam.enabled = false;
             ArmanaCam.GetComponent <AudioListener>().enabled = false;
         }
 
         if (PlayerActive) {
             if (Input.GetButton ("Jump") && playerController.isGrounded) {
                 playerController.Move (Vector3.up * jumpHeight);
             }
 
             transform.Rotate (0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
             Vector3 forward = transform.TransformDirection (Vector3.forward);
             float speed = forwardSpeed * Input.GetAxis ("Vertical");
             playerController.SimpleMove (speed * forward);
         }
     }
 }
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 JerryC92 · Sep 24, 2017 at 07:28 AM

@Durakken I'm not sure if you ever figured out how to solve this issue of yours. I ran into a similar problem with my own project. I was instantiating some vehicles with cameras attached but inactive. I wanted to pick a vehicle out of the various spawned ones to control, but needed to have the desired cameras become active and I found that I could not do this from the spawn script directly. I eventually got it to work by attaching an "EnableCameras" script to the vehicle prefab that looked like this:

public class EnableCameras : MonoBehaviour {

     [SerializeField] public Camera RightRear;
     [SerializeField] public Camera LeftRear;

     void Update()
     {
         if (Input.GetKeyDown(KeyCode.D))
         {
             RightRear.GetComponent<Camera>().enabled = true;
             LeftRear.GetComponent<Camera>().enabled = true;
         }
     }

Then in my spawner script, I enabled the "EnableCameras" script whenever the player decides to take control of their desired vehicle:

 whatToSpawnClone[j].GetComponent<UnityStandardAssets.Vehicles.Car.EnableCameras>().enabled = true;

Perhaps not the most elegant solution, but it did work for the purpose of activating the cameras in a particular instantiated prefab.

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

63 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

Related Questions

Setting a gameObject active when a button is pressed 2 Answers

SetActive not working as should 0 Answers

How do I get an instantiated button to active/de-active a gameobject 0 Answers

disable gameobject after rotation limit 1 Answer

How do you set Prefabs into a GameObject array that will activate/deactivate when called? 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