- Home /
Changing a weapon gun into sword guy
hey hi all i wanted to change the guy in survival shooter into a girl who has sword but my code is not working since it gives error MissingComponentException: There is no 'LineRenderer' attached to the "Player" game object, but a script is trying to access it. You probably need to add a LineRenderer to the game object "Player". Or your script needs to check if the component is attached before using it. Player Shooting. Disable Effects () (at Assets/Scripts/Scripts/Player/Player Shooting.cs:49) PlayerShooting.Update () (at Assets/Scripts/Scripts/Player/PlayerShooting.cs:42) here is my code for player shooting using UnityEngine;
public class PlayerShooting : MonoBehaviour { public int damagePerShot = 20; public float timeBetweenBullets = 0.15f; public float range = 100f;
float timer;
Ray shootRay = new Ray();
RaycastHit shootHit;
int shootableMask;
ParticleSystem gunParticles;
LineRenderer gunLine;
AudioSource gunAudio;
Light gunLight;
float effectsDisplayTime = 0.2f;
void Awake ()
{
shootableMask = LayerMask.GetMask ("Shootable");
gunParticles = GetComponent<ParticleSystem> ();
gunLine = GetComponent <LineRenderer> ();
gunAudio = GetComponent<AudioSource> ();
gunLight = GetComponent<Light> ();
}
void Update ()
{
timer += Time.deltaTime;
if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0)
{
Shoot ();
}
if(timer >= timeBetweenBullets * effectsDisplayTime)
{
DisableEffects ();
}
}
public void DisableEffects ()
{
gunLine.enabled = false;
gunLight.enabled = false;
}
void Shoot ()
{
timer = 0f;
gunAudio.Play ();
gunLight.enabled = true;
gunParticles.Stop ();
gunParticles.Play ();
gunLine.enabled = true;
gunLine.startWidth = 0.02f;
gunLine.SetPosition (0, transform.position);
shootRay.origin = transform.position;
shootRay.direction = transform.forward;
if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
{
EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
if(enemyHealth != null)
{
enemyHealth.TakeDamage (damagePerShot, shootHit.point);
}
gunLine.SetPosition (1, shootHit.point);
}
else
{
gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
}
}
}
Answer by TheSOULDev · Sep 01, 2017 at 07:23 PM
Did you even read the error? Remove access to LineRenderer in the script if you don't have it.