Hey guys!
I’m busy following a space shooter tutorial on YouTube and pretty much following the guy’s steps verbatim. However, I’m experiencing an issue in my Laser.cs script that the guy on YouTube (older version of Unity) doesn’t seem to be experiencing…
My exact issue came in when I started adding fireDelay, which is supposed to add a 2 second delay between my laser shots, but in actuality is causing my laser to only fire once and never again. The error in Unity suggests that the “Invoke” method can’t be called in the script… Any help would mean a lot!
I’m super new to this - this is my first time scripting. Please be gentle with me! XD
Here is the whole script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent (typeof(Light))]
[RequireComponent(typeof(LineRenderer))]
public class Laser : MonoBehaviour {
[SerializeField]float LaserOffTime = .5f;
[SerializeField]float maxDistance = 300f;
[SerializeField]float fireDelay = 2f;
LineRenderer lr;
Light laserLight;
bool canFire;
void Awake()
{
lr = GetComponent<LineRenderer>();
laserLight = GetComponent<Light>();
}
void Start()
{
lr.enabled = false;
laserLight.enabled = false;
canFire = true;
}
public void FireLaser(Vector3 targetPosition)
{
if(canFire)
{
lr.SetPosition(0, transform.position);
lr.SetPosition(1, targetPosition);
lr.enabled = true;
laserLight.enabled = true;
canFire = false;
Invoke("TurnOffLaser", LaserOffTime);
Invoke("Canfire", fireDelay);
}
}
void TurnOffLaser()
{
lr.enabled = false;
laserLight.enabled = false;
}
public float Distance
{
get { return maxDistance; }
}
void CanFire()
{
canFire = true;
}
}