Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

UnityEngine.Component does not contain a definition for isOrthoGraphic

Discussion in 'Scripting' started by AggelosNt, Sep 21, 2016.

  1. AggelosNt

    AggelosNt

    Joined:
    Sep 21, 2016
    Posts:
    1
    I was trying to add a script from unity's Youtube channel regarding camera pinch zoom. I copied everything from the video in my script but it didn't because of the message "UnityEngine.Component does not contain a definition for" and everything that had the word orthographic was after that.
    Here is the script:

    using UnityEngine;

    public class PinchZoom : MonoBehaviour
    {
    public float perspectiveZoomSpeed = 0.5f; // The rate of change of the field of view in perspective mode.
    public float orthoZoomSpeed = 0.5f; // The rate of change of the orthographic size in orthographic mode.


    void Update()
    {
    // If there are two touches on the device...
    if (Input.touchCount == 2)
    {
    // Store both touches.
    Touch touchZero = Input.GetTouch(0);
    Touch touchOne = Input.GetTouch(1);

    // Find the position in the previous frame of each touch.
    Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
    Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;

    // Find the magnitude of the vector (the distance) between the touches in each frame.
    float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
    float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;

    // Find the difference in the distances between each frame.
    float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;

    // If the camera is orthographic...
    if (camera.isOrthoGraphic)
    {
    // ... change the orthographic size based on the change in distance between the touches.
    camera.orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed;

    // Make sure the orthographic size never drops below zero.
    camera.orthographicSize = Mathf.Max(camera.orthographicSize, 0.1f);
    }
    else
    {
    // Otherwise change the field of view based on the change in distance between the touches.
    camera.fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed;

    // Clamp the field of view to make sure it's between 0 and 180.
    camera.fieldOfView = Mathf.Clamp(camera.fieldOfView, 0.1f, 179.9f);
    }
    }
    }
    }




    Please help me
     
  2. gibbie_learnersedge

    gibbie_learnersedge

    Joined:
    Aug 11, 2016
    Posts:
    25
  3. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Make sure to check this out for future problems, you'll get more help with readable code:
    http://forum.unity3d.com/threads/using-code-tags-properly.143875/

    Also you want to check out the actual Camera Class here:
    https://docs.unity3d.com/ScriptReference/Camera.html

    I checked and it seems like all the properties are correct in your code except this line:
    Code (CSharp):
    1. if (camera.isOrthoGraphic)
    Camera doesn't have a variable called isOrthoGraphic.. the documentation shows it should be this:
    Code (CSharp):
    1. if (camera.orthographic)
    (Notice in your code the variable shows up blue , because the forums thinks its a variable we made up. orthographic is purplish and is an actual html link to the real variable)

    Also where is camera being set? Its not in your code, but I assume your assigning the camera variable somewhere earlier in the script in Awake,Start or in the Editor?
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
    Component.camera is a property! It's declared as Component, and is deprecated.
     
    Kiwasi likes this.
  5. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Oh is it part of all the old built in stuff they replaced with GetComponent?
    You could try changing all the camera.xxx to Camera.main.xxx Just make sure the camera in your hierarchy as the tag "Main Camera" it should be default
     
    matthewcurran likes this.
  6. DrumLord

    DrumLord

    Joined:
    Sep 29, 2016
    Posts:
    1
    You simply solve this by changing all your camera.xxxx to Camera.xxxx...

    Hope this helps
     
  7. zeb33

    zeb33

    Joined:
    Nov 17, 2014
    Posts:
    95
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PinchZoom : MonoBehaviour
    4. {
    5.     public float perspectiveZoomSpeed = 0.5f;        // The rate of change of the field of view in perspective mode.
    6.     public float orthoZoomSpeed = 0.5f;        // The rate of change of the orthographic size in orthographic mode.
    7.  
    8.  
    9.     void Update()
    10.     {
    11.         // If there are two touches on the device...
    12.         if (Input.touchCount == 2)
    13.         {
    14.             // Store both touches.
    15.             Touch touchZero = Input.GetTouch(0);
    16.             Touch touchOne = Input.GetTouch(1);
    17.  
    18.             // Find the position in the previous frame of each touch.
    19.             Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
    20.             Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
    21.  
    22.             // Find the magnitude of the vector (the distance) between the touches in each frame.
    23.             float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
    24.             float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
    25.  
    26.             // Find the difference in the distances between each frame.
    27.             float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
    28.  
    29.             // If the camera is orthographic...
    30.             if (GetComponent<Camera>().orthographic)
    31.             {
    32.                 // ... change the orthographic size based on the change in distance between the touches.
    33.                 Camera.main.orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed;
    34.  
    35.                 // Make sure the orthographic size never drops below zero.
    36.                 Camera.main.orthographicSize = Mathf.Max(GetComponent<Camera>().orthographicSize, 0.1f);
    37.             }
    38.             else
    39.             {
    40.                 // Otherwise change the field of view based on the change in distance between the touches.
    41.                 Camera.main.fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed;
    42.  
    43.                 // Clamp the field of view to make sure it's between 0 and 180.
    44.                 Camera.main.fieldOfView = Mathf.Clamp(GetComponent<Camera>().fieldOfView, 0.1f, 179.9f);
    45.             }
    46.         }
    47.     }
    48. }
     
  8. TamoghnaSaha

    TamoghnaSaha

    Joined:
    Jul 31, 2017
    Posts:
    1
    This worked like a charm!
     
    Ary0 and Jen_Alice4Real like this.