• 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
Question by kityanlam3 · May 04, 2016 at 07:45 AM · getcomponentvariablesdifferent objects

Disable function in another script

I have two scripts, one in a camera and another on a prefab. The drag N drop script uses mousewheel to rotate the object that has mouse held down on it w$$anonymous$$le the camera also uses mousewheel to scroll in and out, so it zooms and rotates at the same time. I want to disable the camera w$$anonymous$$le player rotates the object. I've tried using GetComponent and been getting a headache.

T$$anonymous$$ngs to note: The objects are prefabs that are instantiated that the player can pick up and rotate freely.

The camera is the standardassets fps camera.

Drag N Drop using UnityEngine; using System.Collections; using UnityStandardAssets.Characters.FirstPerson;

 public class DragNDrop : MonoBehaviour {
 
     private Vector3 screenPoint;
     private Vector3 offset;
     private bool isPickedup;
     public GameObject targettedObject;
     public GameObject colorcanvas;
     private bool canvasisopen;
     public bool isrotate;
     public float lerpSpeed;
     public Quaternion fromRotation;
     public Quaternion toRotation;
     public float xDeg, yDeg;
     public int speed = 200;
     public float friction;
     RaycastHit $$anonymous$$t;
     //public Camera maincam;
    
     //void OnMouseDown()
     //{
     //    //Vector3 mousePosition = new Vector3(Input.mousePosition.x, 0, Input.mousePosition.z);
     //    //Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
     //    //transform.position = objPosition;
     //    Vector3 mouse = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z);
     //    offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(mouse);
     //}
     void Start()
     {
         
         //colorcanvas = GetComponentInC$$anonymous$$ldren<GameObject>()
     }
     void Update()
     {
 
         //isrotate=maincam.GetComponent<CameraController>().IsRotate;
 
         //if (Input.GetMouseButton(1) && Input.GetKey(KeyCode.Q))
         //{
         //    xDeg -= Input.GetAxis("Mouse X") * speed * friction;
         //    yDeg += Input.GetAxis("Mouse Y") * speed * friction;
         //    Quaternion fromRotation = transform.rotation;
         //    Quaternion toRotation = Quaternion.Euler(yDeg, xDeg, 0);
         //    transform.rotation = Quaternion.Lerp(fromRotation, toRotation, Time.deltaTime * 5.0f);
         //}
         //if (canvasisopen = false && Input.GetMouseButtonDown(2))
         //{
         //    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         //    RaycastHit $$anonymous$$t;
         //    Physics.Raycast(ray, out $$anonymous$$t);
         //    targettedObject = $$anonymous$$t.transform.gameObject;
         //    colorcanvas.SetActive(true);
         //    canvasisopen = true;
         //}
         //if(canvasisopen = true && Input.GetMouseButtonDown(2)  )
         //{
         //    colorcanvas.SetActive(false);
         //    canvasisopen = false;
         //}
         Debug.Log(isrotate);
 
     }
     
             
     
     void OnTriggerEnter(Collider other){
         t$$anonymous$$s.gameObject.transform.position = new Vector3(0, 0, 0);
     }
 
     void OnMouseDrag()
     {
         
             Plane plane = new Plane(Vector3.up, new Vector3(0, 0, 0));
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             Physics.Raycast(ray, out $$anonymous$$t);
 
             float distance;
             if (plane.Raycast(ray, out distance) && $$anonymous$$t.transform.tag != "WallArea")
             {
             
             transform.position =  new Vector3 (ray.GetPoint(distance).x,0,ray.GetPoint(distance).z);
 
             if (Input.GetAxis("Mouse ScrollWheel") > 0)
             {
                 isrotate = true;
                 transform.Rotate(0, Input.GetAxis("Mouse ScrollWheel") * speed, 0);
                 
             }
             else if(Input.GetAxis("Mouse ScrollWheel") < 0)
             {
                 isrotate = true;
                 transform.Rotate(0, -Input.GetAxis("Mouse ScrollWheel") * -speed, 0);
             }
             else{
                 isrotate = false;
             }
             
             
             
         }
 
             
             if (Input.GetKeyDown(KeyCode.Delete))
             {
                 Destroy(t$$anonymous$$s.gameObject);
             }
             //if (Input.GetKeyDown(KeyCode.T))
             //{
             //    GetComponent<Renderer>().material.color = new Color(0, 0, 0);
             //}
         
     }
     }

CameraController using UnityEngine; using System.Collections; namespace UnityStandardAssets.Characters.FirstPerson { public class CameraController : MonoBehaviour {

         int cameraVelocity = 10;
         int zoomspeed = 100;
         public float minX = -360.0f;
         public float maxX = 360.0f;
 
         public float minY = -45.0f;
         public float maxY = 45.0f;
 
         public float sensX = 300.0f;
         public float sensY = 300.0f;
 
         float rotationY = 0.0f;
         float rotationX = 0.0f;
         public GameObject _player;
         public int rotationstart=90;
         public float dragSpeed = 2;
         private Vector3 dragOrigin;
         public float turnSpeed = 2.0f;      // Speed of camera turning when mouse moves in along an axis
         public float panSpeed = 2.0f;       // Speed of the camera when being panned
         public float zoomSpeed = 2.0f;      // Speed of the camera going back and forth
 
         private Vector3 mouseOrigin;    // Position of cursor when mouse dragging starts
         private bool isPanning;     // Is the camera being panned?
         private bool isRotating;    // Is the camera being rotated?
         private bool isZooming;        // Is the camera zooming?
         private bool haspressed;
         public DragNDrop _object;
         public bool isrot;
         public GameObject[] furniture;
         // Use t$$anonymous$$s for initialization
         void Start()
         {
 
         }
 
         // Update is called once per frame
         void Update()
         {
             furniture = GameObject.FindGameObjectsWithTag("GroundFurniture");
 
             if ((Input.GetKey(KeyCode.A)))
             {
                 transform.Translate((Vector3.left * cameraVelocity) * Time.deltaTime);
             }
             if ((Input.GetKey(KeyCode.D)))
             {
                 transform.Translate((Vector3.right * cameraVelocity) * Time.deltaTime);
             }
             if ((Input.GetKey(KeyCode.W)))
             {
                 transform.Translate((Vector3.up * cameraVelocity) * Time.deltaTime);
             }
             if ((Input.GetKey(KeyCode.S)))
             {
                 transform.Translate((Vector3.down * cameraVelocity) * Time.deltaTime);
             }
             
             
             if (Input.GetMouseButton(1))
             {
                 mouseOrigin = Input.mousePosition;
                 rotationX += Input.GetAxis("Mouse X") * sensX * Time.deltaTime;
                 rotationY += Input.GetAxis("Mouse Y") * sensY * Time.deltaTime;
                 
                 transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
 
             }
 
 
             if (_object.isrotate == false)
             {
                 if (Input.GetAxis("Mouse ScrollWheel") > 0)
                 {
                     transform.Translate(Vector3.forward * zoomspeed * Time.deltaTime);
                 }
                 if (Input.GetAxis("Mouse ScrollWheel") < 0)
                 {
                     transform.Translate(Vector3.back * zoomspeed * Time.deltaTime);
                 }
             }
 
             if (Input.GetKeyDown(KeyCode.Z))
             {
                 _player.SetActive(false);
                 
             }
         }
     }
 }
 
Comment

People who like this

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

Answer by meat5000 · May 04, 2016 at 07:59 AM

From what I read, your problem is that you have a headache.

You need to fill out more information on the actual problem.

To access a Camera you must realise that a Camera is not a GameObject but a Component on a GameObject. In the situations where the camera shortcuts dont seem to be working, use GetComponent to access the Camera component on the Camera's GameObject. I have to admit, I find the shortcuts dont work sometimes but once you have a reference to a camera in your scene it seems you can access all the cameras through that structure.

Now to the problem:

If you rotate the camera by script you can easily add a bool switch/flag to control whether the function processes any code or not.

 void MyRotationFunc()
 {
     if (isActivated)
     {
         //Rotate
     }
 }

"isActivated" bool can then be easily controlled from anywhere using GetComponent.

You can put t$$anonymous$$s clause on the function's call rather than inside the function if you choose, but I have a habit of writing generic functions I can fire from anywhere, so I'd want each call to perform the same despite where is was invoked from.

Edit : Here's how I t$$anonymous$$nk you want your scripts to work.

Camera Controller (Just the bits I added to your script)

      //Omitted Code
      public bool cameraFreezeRot = false;

      void Update()
      {
          //Omitted Code
          if (!cameraFreezeRot)
          {
              if (Input.GetAxis("Mouse ScrollWheel") > 0)
              {
                  transform.Translate(Vector3.forward * zoomspeed * Time.deltaTime);
              }
              if (Input.GetAxis("Mouse ScrollWheel") < 0)
              {
                  transform.Translate(Vector3.back * zoomspeed * Time.deltaTime);
              }
          }
          //Omitted Code
      }

DragNDrop:

 using UnityEngine;
 using System.Collections;
 using UnityStandardAssets.Characters.FirstPerson;
 
 public class DragNDrop : MonoBehaviour
 {
     public bool isrotate;
     public int speed = 200;
     RaycastHit $$anonymous$$t;
     private CameraController myCam; //ADDED
 
     void Start()
     {
         myCam = GameObject.FindWithTag("MainCamera").GetComponent<CameraController>(); //ADDED
     }
 
     void OnTriggerEnter(Collider other)
     {
         t$$anonymous$$s.gameObject.transform.position = new Vector3(0, 0, 0);
     }
     
     void OnMouseDrag()
     {
         myCam.cameraFreezeRot = true; //ADDED
         Plane plane = new Plane(Vector3.up, new Vector3(0, 0, 0));
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         Physics.Raycast(ray, out $$anonymous$$t);
 
         float distance;
         if (plane.Raycast(ray, out distance) && $$anonymous$$t.transform.tag != "WallArea")
         {
             transform.position =  new Vector3 (ray.GetPoint(distance).x,0,ray.GetPoint(distance).z);
             if (Input.GetAxis("Mouse ScrollWheel") > 0)
             {
                 isrotate = true;
                 transform.Rotate(0, Input.GetAxis("Mouse ScrollWheel") * speed, 0);
             }
             else if(Input.GetAxis("Mouse ScrollWheel") < 0)
             {
                 isrotate = true;
                 transform.Rotate(0, -Input.GetAxis("Mouse ScrollWheel") * -speed, 0);
             }
             else
             {
                 isrotate = false;
             }
         }
 
         if (Input.GetKeyDown(KeyCode.Delete))
         {
             Destroy(t$$anonymous$$s.gameObject);
         }
     }
     
     void OnMouseUp() //ADDED
     {
         myCam.cameraFreezeRot = false;
     }
 }
Comment
kityanlam3

People who like this

1 Show 2 · 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 kityanlam3 · May 04, 2016 at 08:28 AM 0
Share

Sorry if I was a bit vague on the problem but i've tried your method and created a bool in the camera script. But now I want to access the component from my prefab. Do point out if I'm messing up or something.

 public bool cameraactive;
         // Use this for initialization
         void Start()
         {
             cameraactive = true;
         }
 if (cameraactive == true)
             {
                 if (Input.GetAxis("Mouse ScrollWheel") > 0)
                 {
                     transform.Translate(Vector3.forward * zoomspeed * Time.deltaTime);
                 }
                 if (Input.GetAxis("Mouse ScrollWheel") < 0)
                 {
                     transform.Translate(Vector3.back * zoomspeed * Time.deltaTime);
                 }
             }

^ is saying that the camera will be on all the time except when the player has selected an object and is using the scroll wheel to rotate the object.

 public Camera camera;
         void Start()
         {
             camera= GetComponent<Camera>
             cameraActive = camera.GetComponent<CameraController>().cameraactive;
 

^ Still incomplete but I want to access the camera bool and disable it in the prefab so that I can use my prefabs rotate function without the camera zoom in. Original problem was because both used scroll wheel input, prefab for rotation, camera for zoom in. I wanted to disable camera when player selected prefab.

Down here is where I want to set it to false so it deactivates.

 void OnMouseDrag()
     {
         cameraActive = false;
         Plane plane = new Plane(Vector3.up, new Vector3(0, 0, 0));
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             Physics.Raycast(ray, out hit);
 
             float distance;
         if (plane.Raycast(ray, out distance) && hit.transform.tag != "WallArea")
         {
 
             transform.position = new Vector3(ray.GetPoint(distance).x, 0, ray.GetPoint(distance).z);
 
 
             
                 if (Input.GetAxis("Mouse ScrollWheel") > 0)
                 {
                 cameraActive = false;
                 transform.Rotate(0, Input.GetAxis("Mouse ScrollWheel") * speed, 0);
 
                 }
                 else if (Input.GetAxis("Mouse ScrollWheel") < 0)
                 {
                 
                 transform.Rotate(0, -Input.GetAxis("Mouse ScrollWheel") * -speed, 0);
                 }
 
             }

avatar image kityanlam3 · May 05, 2016 at 03:17 AM 0
Share

My headache is gone, thanks to you. Thank you very much.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Access script based on which one is active 2 Answers

{SOLVED, but not realy} working on a space shooter game and im having problems with my upgrade buttons. 1 Answer

Pass Variable Through C Script 0 Answers

Why can't I access a variable from a different script? 0 Answers

Copying variables in game? 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