Search Unity

Why won't this script work?

Discussion in 'Scripting' started by Treasureman, Nov 27, 2015.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I have a code that lerps a certain variable in a certain script. Here's the code...
    Code (JavaScript):
    1. var NormalY: float = 0; // XOffset while not aiming
    2. var AimY: float = 0.2; // XOffset while aiming
    3. private var cameraScript: MouseOrbitOTS;
    4. function Start(){
    5.      cameraScript = GetComponent(MouseOrbitOTS);
    6.      var ch:Camera = GetComponent(Camera);
    7. }
    8. function Update(){
    9.      var cameraY = NormalY;
    10.      if (Input.GetButton("Aim")){
    11.          cameraY = AimY;
    12.       }
    13.       cameraScript.yOffset = cameraY; // set XOffset while aiming or not aiming
    14. }
    And here's the script it functions with...
    Code (JavaScript):
    1. var target : Transform;
    2. var distance = 10.0;
    3.  
    4. var xOffset = 0.0;
    5. var yOffset = 0.0;
    6. var xSpeed = 250.0;
    7. var ySpeed = 120.0;
    8. var yMinLimit = -20;
    9. var yMaxLimit = 80;
    10. var distanceMin = 3;
    11. var distanceMax = 15;
    12. private var x = 0.0;
    13. private var y = 0.0;
    14. @script AddComponentMenu("Camera-Control/Mouse Orbit")
    15. function Start () {
    16.     var angles = transform.eulerAngles;
    17.     x = angles.y;
    18.     y = angles.x;
    19.     // Make the rigid body not change rotation
    20.        if (rigidbody)
    21.         rigidbody.freezeRotation = true;
    22. }
    23. function LateUpdate () {
    24.     if (target) {
    25.         x += Input.GetAxis("Mouse X") * xSpeed * distance* 0.02;
    26.         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
    27.          y = ClampAngle(y, yMinLimit, yMaxLimit);
    28.         var rotation = Quaternion.Euler(y, x, 0);
    29.         distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);
    30.         var hit : RaycastHit;
    31.         if (Physics.Linecast (target.position, transform.position, hit)) {
    32.                 distance -=  hit.distance;
    33.         }
    34.         var position = rotation * Vector3(xOffset, yOffset, -distance) + target.position;
    35.         transform.rotation = rotation;
    36.         transform.position = position;
    37.     }
    38. }
    39. static function ClampAngle (angle : float, min : float, max : float) {
    40.     if (angle < -360)
    41.         angle += 360;
    42.     if (angle > 360)
    43.         angle -= 360;
    44.     return Mathf.Clamp (angle, min, max);
    45. }
    It works fine, but I went to change it so it would function with another script, and I got an error. Here's the changed version of the first script...
    Code (JavaScript):
    1. var NormalY: float = 0; // XOffset while not aiming
    2. var AimY: float = 0.2; // XOffset while aiming
    3. private var cameraScript: GunSway;
    4. function Start(){
    5.      cameraScript = GetComponent(GunSway);
    6.      var ch:Camera = GetComponent(Camera);
    7. }
    8. function Update(){
    9.      var cameraY = NormalY;
    10.      if (Input.GetButton("Fire2")){
    11.          cameraY = AimY;
    12.       }
    13.       cameraScript.defaultPosition.y = cameraY; // set XOffset while aiming or not aiming
    14. }
    And here's the script I want it to work with...
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GunSway: MonoBehaviour {
    5.     public float moveAmount = 1f;
    6.     public float moveSpeed = 2f;
    7.     public GameObject gun;
    8.     private float moveOnX;
    9.     private float moveOnY;
    10.     public Vector3 defaultPosition;
    11.     public Vector3 newGunPosition;
    12.    
    13.    
    14.     // Use this for initialization
    15.     void Start () {
    16.        
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.        
    22.         moveOnX = Input.GetAxis("Mouse X") * Time.deltaTime * moveAmount;
    23.        
    24.         moveOnY = Input.GetAxis("Mouse Y") * Time.deltaTime * moveAmount;
    25.        
    26.         newGunPosition = new Vector3 (defaultPosition.x+moveOnX, defaultPosition.y+moveOnY, defaultPosition.z);
    27.        
    28.         gun.transform.localPosition = Vector3.Lerp(gun.transform.localPosition, newGunPosition, moveSpeed*Time.deltaTime);
    29.        
    30.     }
    31.    
    32.    
    33. }
    34.  
    I got this error when I changed the script...

    Assets/AimLerpY.js(3,27): BCE0018: The name 'GunSway' does not denote a valid type ('not found').

    Why do I get this if it worked fine with a different script?
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190