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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Animation error script

Discussion in 'Animation' started by ARares, Mar 19, 2016.

  1. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    I have that error to my script.





    My script:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerShooting : MonoBehaviour
    4. {
    5.     public int damagePerShot = 20;                  // The damage inflicted by each bullet.
    6.     public float timeBetweenBullets = 0.15f;        // The time between each shot.
    7.     public float range = 100f;                      // The distance the gun can fire.
    8.  
    9.     public Transform shell;
    10.     public Transform shellEjection;
    11.     //MuzzleFlash muzzleflash;
    12.  
    13.     [SerializeField]
    14.     public GameObject weaponGFX;
    15.  
    16.     [SerializeField]
    17.     private LayerMask mask;
    18.  
    19.     [SerializeField]
    20.     private string weaponLayerName = "Weapon";
    21.  
    22.     float timer;                                    // A timer to determine when to fire.
    23.     Ray shootRay;                                   // A ray from the gun end forwards.
    24.     RaycastHit shootHit;                            // A raycast hit to get information about what was hit.
    25.     int shootableMask;                              // A layer mask so the raycast only hits things on the shootable layer.
    26.     //ParticleSystem gunParticles;                    // Reference to the particle system.
    27.     LineRenderer gunLine;                           // Reference to the line renderer.
    28.     AudioSource gunAudio;                           // Reference to the audio source.
    29.     Light gunLight;                                 // Reference to the light component.
    30.     float effectsDisplayTime = 0.2f;                // The proportion of the timeBetweenBullets that the effects will display for.
    31.  
    32.     private WeaponGraphics currentGraphics;
    33.  
    34.     public ParticleSystem muzzleFlash;
    35.  
    36.     public Animator pistol1;
    37.  
    38.     public WeaponGraphics GetCurrentGraphics()
    39.     {
    40.         return currentGraphics;
    41.     }
    42.  
    43.     void Start()
    44.     {
    45.         //muzzleflash = GetComponent<MuzzleFlash>();
    46.         currentGraphics = GetComponent<WeaponGraphics>();
    47.  
    48.         weaponGFX.layer = LayerMask.NameToLayer(weaponLayerName);
    49.  
    50.         pistol1 = GetComponent<Animator>();
    51.     }
    52.  
    53.     void Awake()
    54.     {
    55.         // Create a layer mask for the Shootable layer.
    56.         shootableMask = LayerMask.GetMask("Shootable");
    57.  
    58.         // Set up the references.
    59.         //gunParticles = GetComponent<ParticleSystem>();
    60.         gunLine = GetComponent<LineRenderer>();
    61.         gunAudio = GetComponent<AudioSource>();
    62.         gunLight = GetComponent<Light>();
    63.     }
    64.  
    65.     void Update()
    66.     {
    67.         // Add the time since Update was last called to the timer.
    68.         timer += Time.deltaTime;
    69.  
    70.         // If the Fire1 button is being press and it's time to fire...
    71.         if (Input.GetButton("Fire1") && timer >= timeBetweenBullets)
    72.         {
    73.             // ... shoot the gun.
    74.             Shoot();
    75.         }
    76.  
    77.         // If the timer has exceeded the proportion of timeBetweenBullets that the effects should be displayed for...
    78.         if (timer >= timeBetweenBullets * effectsDisplayTime)
    79.         {
    80.             // ... disable the effects.
    81.             DisableEffects();
    82.         }
    83.     }
    84.  
    85.     public void DisableEffects()
    86.     {
    87.         // Disable the line renderer and the light.
    88.         gunLine.enabled = false;
    89.         gunLight.enabled = false;
    90.     }
    91.  
    92.     void Shoot()
    93.     {
    94.         // Reset the timer.
    95.         timer = 0f;
    96.  
    97.         // Play the gun shot audioclip.
    98.         gunAudio.Play();
    99.  
    100.         // Enable the light.
    101.         gunLight.enabled = true;
    102.  
    103.         //Instantiate(shell, shellEjection.position, shellEjection.rotation);
    104.  
    105.         muzzleFlash.Stop();
    106.         muzzleFlash.Play();
    107.  
    108.         bool fire = Input.GetButtonDown("TestButton");
    109.  
    110.         pistol1.SetBool("IsShooting", fire);
    111.  
    112.         // Enable the line renderer and set it's first position to be the end of the gun.
    113.         //gunLine.enabled = true;
    114.         gunLine.SetPosition(0, transform.position);
    115.  
    116.         // Set the shootRay so that it starts at the end of the gun and points forward from the barrel.
    117.         shootRay.origin = transform.position;
    118.         shootRay.direction = transform.forward;
    119.  
    120.         // Perform the raycast against gameobjects on the shootable layer and if it hits something...
    121.         if (Physics.Raycast(shootRay, out shootHit, range, shootableMask))
    122.         {
    123.             // Try and find an EnemyHealth script on the gameobject hit.
    124.             EnemyHealth enemyHealth = shootHit.collider.GetComponent<EnemyHealth>();
    125.  
    126.             // If the EnemyHealth component exist...
    127.             if (enemyHealth != null)
    128.             {
    129.                 // ... the enemy should take damage.
    130.                 enemyHealth.TakeDamage(damagePerShot, shootHit.point);
    131.             }
    132.  
    133.             // Set the second position of the line renderer to the point the raycast hit.
    134.             gunLine.SetPosition(1, shootHit.point);
    135.         }
    136.         // If the raycast didn't hit anything on the shootable layer...
    137.         else
    138.         {
    139.             // ... set the second position of the line renderer to the fullest extent of the gun's range.
    140.             gunLine.SetPosition(1, shootRay.origin + shootRay.direction * range);
    141.         }
    142.     }
    143. }
    I need help please.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,998
    if you remove line 50, does it work then?
    Code (CSharp):
    1. pistol1 = GetComponent<Animator>();
     
  3. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    If I remove line 50, the animation not working. What i want is to make recoil.
     
  4. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    As the error says, Attach a Animator component to the 'muzzle' GameObject
     
  5. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    When Attach a Animator, animation not working.
     
  6. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    I fixed